读书人

dataGridView动态添加行新行排名首先

发布时间: 2013-09-05 16:02:07 作者: rapoo

dataGridView动态添加行,新行排名第一行

      DataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = C.time;
row.Cells.Add(textboxcell);
DataGridViewTextBoxCell textboxcel2= new DataGridViewTextBoxCell();
textboxcell.Value = C.temperature;
row.Cells.Add(textboxcel2);


dataGridView1.Rows.Insert(1, row);//这句代码保存提供的行索引超出范围。
参数名: rowIndex

[解决办法]
dataGridView1.Rows.Insert(0, row);

[解决办法]
//要加这两句,添加列
this.dataGridView1.Columns.Add("1", "1");//两个参数,前者为列名,后者为headertext
this.dataGridView1.Columns.Add("2", "2");

DataGridViewRow row = new DataGridViewRow();

DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = 1;
row.Cells.Add(textboxcell);

DataGridViewTextBoxCell textboxcel2 = new DataGridViewTextBoxCell();
textboxcel2.Value = 2;//这里是2,是1的话,就覆盖了
row.Cells.Add(textboxcel2);


dataGridView1.Rows.Insert(0, row);//这里是0,索引从0开始。

读书人网 >C#

热点推荐