我试图将数据作为一行一行添加到datagridview,这是我的代码,它说:
这是什么意思?我的代码有什么问题?
String Sqlstr2 = "select ItemName from Item where ItemID = '" + tbItemID.Text + "'";
db.DataRead(Sqlstr2);
string ItemName = db.dr["ItemName"].ToString();
DataGridView dataGridView1 = new DataGridView();
dataGridView1.Columns[0].Name = "ItemID";
dataGridView1.Columns[1].Name = "ItemName";
dataGridView1.Columns[2].Name = "Qty";
dataGridView1.Columns[3].Name = "UnitPrice";
dataGridView1.Columns[4].Name = "Amount";
string firstColum = tbItemID.Text;
string secondColum = ItemName;
string thirdColum = tbQuantity.Text;
string fourthColum = Convert.ToString(UnitPrice);
string fifthColum = Convert.ToString(sum);
string[] row = new string[]{ firstColum, secondColum, thirdColum, fourthColum, fifthColum };
dataGridView1.Rows.Add(row);
最佳答案
您没有在DataGridView
中添加列
DataGridView dataGridView1 = new DataGridView();//Create new grid
dataGridView1.Columns[0].Name = "ItemID";// refer to column which is not there
现在是否清楚为什么会异常(exception)?
在使用列修复错误之前添加此行
dataGridView1.ColumnCount = 5;
关于c# - 索引超出范围。必须为非负数,并且小于集合参数名称:index的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19162248/