.NET 3.5 Winforms
I have a datagridview bound to a datatable at RUNTIME. There are three columns. The third column is the only editable one. Sometimes the value is free text, sometimes the value is a selection from a combobox, or at least that is the design.
After I bind the data table to the Datagridview with this code:
With dgvColumnFilters
.DataSource = _dtFilter
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.Columns(0).Visible = False
.Columns(0).ReadOnly = True
.Columns(1).ReadOnly = True
.Columns(1).Width = 170
.Columns(1).HeaderCell.Value = "Field"
.Columns(2).Width = 300
.Columns(2).HeaderCell.Value = "Filter List to Value"
I then proceed to iterate the rows of the dgv. If the row requires a combobox, I run code like this:
Select Case sOvrType
Case "NVARCHAR"
' do nothing. The default is a textbox.
Case "YESNO" ' an override type to say that I need to ask YES, NO or show ALL Values
Dim sTest As String = ""
If Not IsDBNull(dgvColumnFilters(2, i).Value) Then
sTest = CStr(dgvColumnFilters(2, i).Value)
Else
sTest = "*"
End If
dgvColumnFilters(2, i) = New DataGridViewComboBoxCell
CType(dgvColumnFilters(2, i), DataGridViewComboBoxCell).DataSource = YesNoDataTable()
CType(dgvColumnFilters(2, i), DataGridViewComboBoxCell).DisplayMember = "display"
CType(dgvColumnFilters(2, i), DataGridViewComboBoxCell).ValueMember = "value"
CType(dgvColumnFilters(2, i), DataGridViewComboBoxCell).Value = sTest
I still get a textbox even though when I step through the above code, the cell shows as a DataGridViewComboBoxCell, the selection value works, and the code throws no errors.
I am completely confused. Can anyone help me get past this? As I said some rows must be text boxes and others drop-down-list comboboxes.
What am I missing?
Thanks, John.
dgvColumnFilters(2, i) = New DataGridViewComboBoxCellthey need to be added to the DGV columns collection and then that column is there for all rows, not just the one you are on. - Ňɏssa Pøngjǣrdenlarp