DataGridView 添加行怎么没效果

谁学逆向工程 2016-10-25 04:15:46
this.dgv.Rows.Add(); 好像发生异常了,单步到这以后直接没执行后面的代码。
dgv 就是 DataGridView
            DataTable dt = new DataTable();//建立个数据表

dt.Columns.Add(new DataColumn("id", typeof(int)));//在表中添加int类型的列
dt.Columns.Add(new DataColumn("Name", typeof(string)));//在表中添加string类型的Name列



dgv.DataSource = dt;

int index = this.dgv.Rows.Add();
this.dgv.Rows[index].Cells[0].Value = 1;
this.dgv.Rows[index].Cells[1].Value = "2";

index = this.dgv.Rows.Add();
this.dgv.Rows[index].Cells[0].Value = 1;
this.dgv.Rows[index].Cells[1].Value = "2";

dgv.DataSource = dt;
...全文
126 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Poopaye 2016-10-25
  • 打赏
  • 举报
回复
因为最后又把dt赋回去了啊
巴士上的邂逅 2016-10-25
  • 打赏
  • 举报
回复
既然给DataGridView的DataSource赋值了,就不要操作DataGridView了,操作DataTable
EnForGrass 2016-10-25
  • 打赏
  • 举报
回复
这样

  dt.Columns.Add("批号", typeof(string));
            dt.Columns.Add("数量", typeof(int));
            DataRow dr1 = dt.NewRow();
            dr1["批号"] = "0001";
            dr1["数量"] = 100;
            dt.Rows.Add(dr1);

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧