【问题标题】:How to display data from an array in a grid in WindowsForms - C#如何在 Windows 窗体的网格中显示数组中的数据 - C#
【发布时间】:2016-06-16 16:53:28
【问题描述】:

我有一个 WindowsForms 应用程序,我正在将其编译为 DLL,以供另一个程序使用。该程序为 DLL 提供了一个填充了信息(浮点数)的数组。我想做的是以有组织的方式(行和列)显示这些数字。到目前为止,我研究的方法结合了 DataGridView 对象的使用,但是我没有“数据库”,我只有将原始数据存储为数组。

执行此操作的唯一方法是使用数组中的数据创建 SQL 数据库吗?或者有更简单/更快的方法吗?

【问题讨论】:

  • 查看DataGridView 的“数据绑定”支持。您可以绑定到多个数据库。

标签: c# arrays winforms visual-studio datagridview


【解决方案1】:

感谢您的回复,但我最后只是循环创建行和列并用我需要的数据填充它们。

创建/初始化 DataGridView:

private System.Windows.Forms.DataGridView dataGridView1;
this.dataGridView1 = new System.Windows.Forms.DataGridView();

//Size and location can be changed to whatever you want
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 436);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowHeadersWidth = 70; //Adjusts row header to fit entire "Score" string
this.dataGridView1.Size = new System.Drawing.Size(976, 398);
this.dataGridView1.TabIndex = 10;

创建行/列并用我的数据填充它们:

for (int i = 0; i <= 40; i++)
{
    dataGridView1.Columns.Add("column" + i.ToString(), i.ToString());
    dataGridView1.Columns[i].Width = 22;
}

DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();        

for (int i = 0; i <= 40; i++)
{
    row.Cells[i].Value = scores[0,i]; // "scores" is an Int32[,] array filled with my data
}


dataGridView1.Rows.Add(row);
dataGridView1.Rows[0].HeaderCell.Value = "Score";

结果类似于this。

【讨论】:

    【解决方案2】:

    您可以发送 XML 格式的 gridview 数据

    private void Form1_Load(object sender, EventArgs e) {
        DataSet ds = new DataSet();
        ds.ReadXml(@"XMLFile1.xml");
        gridControl1.DataSource = ds.Tables[1];
        gridView1.PopulateColumns();
    }
    

    【讨论】:

    • 仅供参考,如果每行缩进 4 个空格,则会得到“块”代码格式。然后你不需要反引号(那些仅用于“内联”代码格式)。我在这里修复了您的答案作为示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多