Reputation: 2865
I am new to winforms and I have a datagridview inside a table control. I am trying to bind it to display data.
DataSet dataSet = new DataSet();
DataTable dataTable = dataSet.Tables.Add("Results");
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Name");
dataTable.Rows.Add("1","Jack");
dataTable.Rows.Add("2","Donna");
dataGridView1.DataSource = dataSet;
I don't find a dataGridView1.DataBind? So I am wondering how I can achieve this?
Also, I'm trying to figure out how to have the first column of the DataGridView as a checkbox. any pointers would help.
Upvotes: 5
Views: 19378
Reputation: 13
you only have to add this:
dataGridView1.DataMember = ds.Tables(0).ToString()
Upvotes: 0
Reputation: 8598
As of note, the link BWC's answer gives off incorrect syntax for referencing a datatable from a dataset. You use []'s not ()'s to reference the index of datatables in a DS.
DataSet dataSet = new DataSet();
DataTable dataTable = dataSet.Tables.Add("Results");
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Name");
dataTable.Rows.Add("1","Jack");
dataTable.Rows.Add("2","Donna");
dataGridView1.DataSource = dataSet.Tables["dataTable"]
If you are looking at checkboxes, but not one that is boundto any data, use the property editor of the DataGridView to edit the columns (click on the elipses "..." in that field)
Click on Add, select unbound column, and finally choose the checkbox column type:
Upvotes: 6
Reputation: 488
http://hodentekhelp.blogspot.com/2008/07/how-to-bind-dataset-to-datagridview.html
This should help with your databinding
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcolumn.aspx
take a look at that for the checkbox column
Here is some sample code
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("Blah",typeof(bool));
dt.Columns.Add("Blah2");
ds.Tables.Add(dt);
dataGridView1.DataSource = ds.Tables[0];
Upvotes: 6
Reputation: 111
Setting DataSource property will bind datasouce. ASP.NET needs a .DataBind call instead.
For the checkbox you need to add a column of type DataGridViewCheckBoxColumn.
Best resource i found on databind (these are for framework 2.0/VS2005):
http://windowsclient.net/Samples/Go%20To%20Market/Data%20Binding/DataBinding%20FAQ.doc
http://windowsclient.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc
Upvotes: 2