I want to add new row to a 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.
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]).Checked
Hope this helps,
Matt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.