desifunde

Hi,

I am using a Datadridviewcomboboxcolumn which is using a childtable-parenttable relationship for a datasource, the data is being added to the datasource at run-time.

Be default all the comboboxes created ( in all rows) show an empty cell, what event/default index setting can I use to display the first item in the combobox for each datagridviewcomboboxcell by default


Thanks



Re: Windows Forms Data Controls and Databinding Default display index of Datagridviewcombobox Cell

Ken Tucker

Post the code you are using to create the combobox column






Re: Windows Forms Data Controls and Databinding Default display index of Datagridviewcombobox Cell

desifunde



Dim prodcolumn As New DataGridViewComboBoxColumn
prodcolumn.Name = "PRODS EXCS"
prodcolumn.HeaderText = "PRODS EXCS"
prodcolumn.DataSource = Me.Prods_BindingSource
prodcolumn.DisplayMember = "PRODSEXCS"
prodcolumn.ValueMember = "MODELNUM"
prodcolumn.DataPropertyName = "MODELNUM"
prodcolumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
prodcolumn.AutoComplete = False
prodcolumn.Width = S_LView_Mdl_Col_Widths(9) * 5
Me.DGView_Models.Columns.Add(prodcolumn)


Thanks







Re: Windows Forms Data Controls and Databinding Default display index of Datagridviewcombobox Cell

desifunde

Do I need to set any other parameters for the column




Re: Windows Forms Data Controls and Databinding Default display index of Datagridviewcombobox Cell

Rong-Chun Zhang - MSFT

Hi desifunde,

Try like this.

Code Snippet

Public Class Form1

Private pdt As DataTable = New DataTable

Private cdt As DataTable = New DataTable

Private Sub For1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

pdt.Columns.Add("order")

pdt.Columns.Add("productid")

pdt.Rows.Add("01")

pdt.Rows.Add("02")

pdt.Rows.Add("03", "002")

pdt.Rows.Add("04")

pdt.Rows.Add("05")

cdt.Columns.Add("productid")

cdt.Columns.Add("name")

cdt.Rows.Add("001", "aaa")

cdt.Rows.Add("002", "bbb")

cdt.Rows.Add("003", "ccc")

cdt.Rows.Add("004", "ddd")

Me.DataGridView1.DataSource = pdt

Me.DataGridView1.Columns.RemoveAt(1)

Dim dgvcob As DataGridViewComboBoxColumn = New DataGridViewComboBoxColumn

dgvcob.HeaderText = "Product"

dgvcob.DataPropertyName = "productid"

dgvcob.DisplayMember = "name"

dgvcob.ValueMember = "productid"

dgvcob.DataSource = cdt

dgvcob.DefaultCellStyle.NullValue = "aaa" 'important

dgvcob.DisplayStyleForCurrentCellOnly = True

Me.DataGridView1.Columns.Add(dgvcob)

End Sub

End Class

Hope this helps.

Regards






Re: Windows Forms Data Controls and Databinding Default display index of Datagridviewcombobox Cell

desifunde

Thanks.

It would be possible to do this if I knew "aaa", but I am populating the datatable at runtime with the form ( with the datagridview) show method not called. Once I call form.show, the datagridview shows up, at this time I want the comboboxes to show the first item by default.

One way to do it would be for me to go through all the comboboxes and then set their nullvalue to the first item, if this is not supported by default

Also how do I get access to the displaymembers of a combobox not the valuemembers.

Thanks much




Re: Windows Forms Data Controls and Databinding Default display index of Datagridviewcombobox Cell

Rong-Chun Zhang - MSFT

Hi desifunde,

Use the DataTable.Rows[0]["displayColumnName"].ToString() instead of a real value("aaa").

Hope this helps.

Regards