Datagridview Setting datasource property of datagridviewcomboboxcell at run time , csharp , datagridview , datagridviewcomboboxcolumn , datagridviewcomboboxcell Vantaa, Finland
  • 12 years ago
    Lets take an example. we have a DataGridView with 2 columns, both columns are combobox columns and are databound. First column contains Company and second column Products. At run time in first column when i select company second column must filled with its products, focus should ramain at first column until i press enter. Moreover when ever second column is refereshed its first value should be selected by default or how can i set it combobox2.SelectedIndex = 0 property at run time while having focus in first column. It should work like this, when i select in first column Company Nestle 2nd column should fill automatically with Nestle's products. like Milk, Kit Kat, Nesveta and so on, and first product Milk should be selected by default, please note that it is required while first Column Company is being edited. my combobox is databound, code flow is something like this. A datagridviewcomboboxcolumn is created on form load event. And its datasource, displaymember, valuemember properties are set, moreover its column’s property .DataPropertyName is also set to related field. When I RUN the form it works fine, it saves all data to table properly, but problem is this, some time I need to fill a particular datagridviewcomboboxcell with different data, so I do following: Dim tempCol2 As DataGridViewComboBoxCell = CType(GView.Rows(0).Cells(1), DataGridViewComboBoxCell) 'Populate tempCol2 with new data With tempCol2 .DataSource = Populate("Select brandsr ,brandname from subcompbrand where brandcompsr=1 and brandgroup=8") .DisplayMember = "brandname" .ValueMember = "brandsr" End With ‘ Set the populated cell back to grid GView.Rows(1).Cells(1) = tempCol2 GView.Rows(1).Cells(1).Value = 0 ‘ above line adds combobox back to datagridview at its place but throws error coz its by default blank(Null) valuemember’s value do not match with .DataPropertyName, how can i set default value for both combobox after refilling by default it displays blank text but contain new data in it. I can dropdown combo to select values and it shows new values in it, I want to do following: - it should not remain blank, it should contain first value of combobox by default - or I may need to set particular listindex of datagridviewcomboboxcell like this: tempCol2.Listindex = 1 to set by default 2nd value in list - if I want to show first value of combobox by default how can I set value to its cell (.DataPropertyName) to save in table if you are not clear I can send to sample form, plz advise you email, thanking you in advance Report post as abusive
  • 12 years ago
    do you mind sending sample form. [email protected]
  • 12 years ago
    hi im trying to send you sample at your email, but its bounced back, can you please confirm your mailling address?

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold