Question

How can I modify the DataGridViewColumn in a DataGridView when using DataGridView.DataSource = DataSet.Table?

OleDbConnection connection = new OleDbConnection(...);
OleDbDataAdapter adpCustomer = new OleDbDataAdapter(..);
DataSet dsCustomer = new DataSet();
adpCustomer.Fill(dsCustomer, "customer");
DataGridView dgv = new DataGridView();
dgv.DataSource = dsCustomer.Tables[0];

// TODO: modify columns to use combobox, checkbox etc.. how?

Thanks in advance.

Was it helpful?

Solution

You cannot modify columns. If you want a ComboBoxColumn, you'll need to add it to the DataGridView before. Add your combo box column BEFORE you bind and set its DataPropertyName so the grid will bind the correct data to that column instead of creating a new one.

Another way to do this is, if you can, to use strong-typed DataSet, because it will automatically create CheckBoxColumns for your bool columns, etc...

OTHER TIPS

Use the datagrid's AutoGeneratingColumn event. This will give you a handle to the column being created and you can alter the column as needed. Below code straight out of VS2010 Help File:

Private Sub DG1_AutoGeneratingColumn(ByVal sender As Object, ByVal e As DataGridAutoGeneratingColumnEventArgs)
    Dim headername As String = e.Column.Header.ToString()
        If headername = "FirstName" Then
        e.Column.Header = "First Name"
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top