Click here to Skip to main content
14,303,339 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a winForm with a dataGridView that has 5 textbox columns and one combobox column. I've successfully binded the combobox to my bindingSource object, now I'd like to bind two other columns to my bindingSource object.

How do I go about binding individual columns? I can't seem to figure how to bind individual textbox columns to my bindingSource.
Posted
Updated 7-Aug-19 2:45am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

DataSource property of DataGridView
and DataPropertyName of DataGridViewColumn as explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.datapropertyname.aspx[^]
can be used for this purpose.

Before setting the DataSource property of DataGridView the AutoGenerateColumns property is to be set to false to avoid the DataGridView populating the Columns automatically.
dataGridView1.AutoGenerateColumns = false;
   
Comments
d.allen101 2-May-12 13:33pm
   
not working. i've already tried to traditional approach and for some reason as soon as my form loads it generates 3 rows
d.allen101 2-May-12 13:51pm
   
how can i databind different columns to different dataSources?
VJ Reddy 2-May-12 13:57pm
   
As far as I know we cannot bind different data sources in a single DataGridView. The reason is that the DataSource property is available at DataGridView level, which means that only one DataSource can be set per DataGridView. Then the DataPropertyName property of DataGridViewColumn can be used to assign a particular field of the DataSource to the DataGridViewColumn.
d.allen101 2-May-12 14:01pm
   
how can i stop the bound dataGridView from adding rows once it's loaded? i've bound it to my bindingSource and specified the column i want to bind to via the dataGrid colunmn's DataPropertyName but when the form loads it automatically creates 3 empty row. i've also set AllowUserToAddRows = false. not sure if that makes a difference
VJ Reddy 2-May-12 14:07pm
   
I don't think empty rows will be added automatically. Only one empty row will be shown to add a new row if AllowUserToAddRows = true.
Please check your DataTable which is connected through the BindingSource. It may be having empty rows. In that case the rows will be shown in DataGridView.
d.allen101 2-May-12 14:09pm
   
thanks...yeah my dataSource is filled. thanks a lot VJ
VJ Reddy 2-May-12 14:15pm
   
You're welcome and thank you for the response.
If the solution and/or discussion is helpful then you may consider to vote and accept the solution.
d.allen101 2-May-12 15:01pm
   
why does this not work. dataGridView.DataBindings.Add("col1",bindingSrc,"Name")
dataGridView.DataBindings.Add("col2",bindingSrc,"Addrs")
dataGridView.DataBindings.Add("col3",bindingSrc, "State"), etc...
VJ Reddy 2-May-12 20:48pm
   
I have not tried but I think the DataBindings property can be used to bind a property on the Control say Text property of TextBox to a DataSource. In this case col1 is not a property of DataGridView. Rather it is a member of the Columns collection of DataGridView.
To know whether it works or not, give a try by giving hat above statements and see what happens.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100