简体   繁体   English

在datagridview中添加新行

[英]Add new row in a datagridview

I want to add new row to a gridview. 我想向gridview添加新行。 The value of each cell comes from the bottom blank cells. 每个单元格的值来自底部的空白单元格。 How to grab the cell's value? 如何获得细胞的价值?

private void AddRow_Click(object sender, EventArgs e)
    {
        DataTable dt = ds.Tables["test"];
        DataRow dr = dt.NewRow();
        // not sure how to add
    }

图片 Please also notice there is a checkbox in a column. 另请注意,一栏中有一个复选框。

Thanks. 谢谢。

The code can be used 该代码可以使用

//define datatable (with all columns you want to have)...
  DataRow dr;
  int lastRow = this.dataGridView1.Rows.Count-2;
  for(int i=0;i<dataGridView1.Columns.Count; i++)
  {
     // grab the values from the last row cells.
     dr[i] = dataGridView1[i, lastRow].Value;
   }
     dataTable.Rows.Add(dr);

Self reslove it. 自我爱它。

DataTable myTable = new DataTable();
DataColumn values = new DataColumn("Test");
myTable.Columns.Add(values);
DataRow rd = myTable.NewRow();
rd["Test"] ="";
myTable.Rows.Add(rd);
datagridview1.DataSource = myTable;

You can add Rows to the datatable by using the Add method. 您可以使用Add方法将Rows添加到数据表中。

dt.Rows.Add(dr);

You can access the values by using the overloaded brackets. 您可以使用重载的括号访问这些值。

dt.Rows[a][b]

will give you the value at a certain row and certain column 将为您提供特定行和特定列的值

To get the value of the Checkbox you can cast the object retrieved from dt.Rows[a][b] to a checkbox and then get the value of the Checked attribute. 要获取Checkbox的值,可以将从dt.Rows [a] [b]中检索到的对象转换为Checkbox,然后获取Checked属性的值。

((CheckBox)dt.Rows[a][b]).Checked

Hope this helps, 希望这可以帮助,

Matt 马特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM