Answered by:
DataGridView bound to datatable with Boolean value

Question
-
Hi there..
I'm having trouble with a simple concept here... (at least i think its simple)
I have a Datatable which is bound to a Datagridview as follows...
SelectSeqGrid.DataSource = GridSRC;
it currently has one column in it. now I want to add a new column within the DataTable with DataType Bool. my question is this...
how can i then have the DataGridview update itself with the structure of the Datatable such that it sees a change within the datatable and puts checkboxes within the corresponding column. any help would be greatly appreciated Thanks!!
Wednesday, September 15, 2010 8:55 PM
Answers
-
This worked for me.
DataTable dt; dt = new DataTable(); dt.Columns.Add(new DataColumn("Name")); dataGridView1.DataSource = dt; dt.Columns.Add(new DataColumn("IsBusiness",typeof(Boolean)));
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!- Proposed as answer by Mitja Bonca Thursday, September 16, 2010 10:59 AM
- Marked as answer by Helen Zhou Friday, September 24, 2010 2:41 AM
Thursday, September 16, 2010 3:41 AM -
Hello Lionoa,
I created a demo about editing DataTable.
First : Create a DataTable.
I created a DataTable in a method named GetDataTable() with 2 columns and 5 rows.private DataTable GetDataTable()
{
// Create an DataTable
DataTable dataTable = new DataTable("MyDataTable");
//Create ID DataColumn
DataColumn dataColumn_ID = new DataColumn("ID", typeof(Int32));
dataTable.Columns.Add(dataColumn_ID);
//Create another DataColumn Name
DataColumn dataColumn_Name = new DataColumn("Name", typeof(string));// You may adjust this type to Bool as DeborahK posted, but the theory is the same.
dataTable.Columns.Add(dataColumn_Name);
//Now add some rows to newly created dataTable
DataRow dataRow;
for (int i = 0; i < 5; i++)
{
dataRow = dataTable.NewRow();// create new row
dataRow["ID"] = i;
dataRow["Name"] = "Name_" + i.ToString();
dataTable.Rows.Add(dataRow);
}
dataTable.AcceptChanges();
return dataTable;
}Second, edit the row in the DataTable:
DataTable dataTable = GetDataTable(); //GetDataTable() method will return you dataTable
dataTable.Rows[0]["ID"] = 10; //Edit row, assign the value
dataTable.AcceptChanges();
dataGridView1.DataSource = dataTable; //assign back to gridviewYou may refer to this article on MSDN How to: Edit Rows in a DataTable
Regards,
Helen Zhou
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.- Marked as answer by Helen Zhou Friday, September 24, 2010 2:41 AM
Tuesday, September 21, 2010 8:28 AM
All replies
-
This worked for me.
DataTable dt; dt = new DataTable(); dt.Columns.Add(new DataColumn("Name")); dataGridView1.DataSource = dt; dt.Columns.Add(new DataColumn("IsBusiness",typeof(Boolean)));
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!- Proposed as answer by Mitja Bonca Thursday, September 16, 2010 10:59 AM
- Marked as answer by Helen Zhou Friday, September 24, 2010 2:41 AM
Thursday, September 16, 2010 3:41 AM -
yes that did work.. what i did do was just unbind and then rebind the dataviewsource.
One more question---> i want to edit an existing rows value within a data-table. what is required to achieve this? i mean what statement? all i see is people adding new rows into the data-table. basically would like to set one or two of the boolean values within the table to true and conversely retrieve those values as required..??
i know this is basic but i am new to all this. Thank you once again!!
Thursday, September 16, 2010 1:45 PM -
Hello Lionoa,
I created a demo about editing DataTable.
First : Create a DataTable.
I created a DataTable in a method named GetDataTable() with 2 columns and 5 rows.private DataTable GetDataTable()
{
// Create an DataTable
DataTable dataTable = new DataTable("MyDataTable");
//Create ID DataColumn
DataColumn dataColumn_ID = new DataColumn("ID", typeof(Int32));
dataTable.Columns.Add(dataColumn_ID);
//Create another DataColumn Name
DataColumn dataColumn_Name = new DataColumn("Name", typeof(string));// You may adjust this type to Bool as DeborahK posted, but the theory is the same.
dataTable.Columns.Add(dataColumn_Name);
//Now add some rows to newly created dataTable
DataRow dataRow;
for (int i = 0; i < 5; i++)
{
dataRow = dataTable.NewRow();// create new row
dataRow["ID"] = i;
dataRow["Name"] = "Name_" + i.ToString();
dataTable.Rows.Add(dataRow);
}
dataTable.AcceptChanges();
return dataTable;
}Second, edit the row in the DataTable:
DataTable dataTable = GetDataTable(); //GetDataTable() method will return you dataTable
dataTable.Rows[0]["ID"] = 10; //Edit row, assign the value
dataTable.AcceptChanges();
dataGridView1.DataSource = dataTable; //assign back to gridviewYou may refer to this article on MSDN How to: Edit Rows in a DataTable
Regards,
Helen Zhou
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.- Marked as answer by Helen Zhou Friday, September 24, 2010 2:41 AM
Tuesday, September 21, 2010 8:28 AM