我目前正在编写一个用于测试PCBA的功能测试(FCT(软件。用户之间的主界面有一个DataGridView,它将用于显示与功能测试有关的所有信息。我也在从LABView过渡到C#来开发这些,所以我还在学习一些。
在应用程序开始时,我想做的是为数据显示一个5列×100行的表。我选择了100行,以使表格填满窗口(这样看起来也会更好一些(。
我遇到的问题是当我试图将数据添加到同一行时。请注意,放入网格中的第一组数据将只在两列中,而稍后添加的数据将跨越所有五列。以下是我现在尝试更新数据网格的内容:
mydatagrid.RowCount = 100; //Put
int rowIndex; //variable for tracking where I'm placing data on the table
//Do some stuff that doesn't involve my datagrid
mydatagrid.Rows[rowIndex].Cells[0].Value = someData; //This line executes fine
mydatagrid.Rows[rowIndex].Cells[1].Value = someOtherData; //This throws an exception
//Do more stuff that doesn't involve my datagrid
我尝试过的另一种更新表格的方法是
mydatagrid.Rows.Add(somedata, someotherdata);
虽然这样做效果很好,但它将数据放在表的末尾,而不是顶部。
此外,稍后在应用程序中也需要这样的东西:
activaterelays.clicketyclackety();
makemeasurement();
updatedatagrid(); // This function will put data in all five columns
也许我错过了一些琐碎的设置,因为这是我第一次尝试使用Windows窗体的DataGridView。
要在DatagridView中的行和单元格中使用索引,必须首先用数据填充。
试试这样的东西:
public class MyData {
public string column1{ get; set;}
public string column2{ get; set;}
public string column3{ get; set;}
public string column4{ get; set;}
public string column5{ get; set;}
}
var data = new List<MyData>() {...}// You must insert the 100 items
//Loads the data to DatagridView, after this line you will see the DatagridView populated.
mydatagrid.DataSource = data;
//Now you can manipulate it
mydatagrid.Rows[rowIndex].Cells[0].Value = someData;
mydatagrid.Rows[rowIndex].Cells[1].Value = otherdata;