开发者

Using DataSet with DataGridView and modifying DataGridViewColumn

开发者 https://www.devze.com 2023-01-30 13:31 出处:网络
How can I modify the DataGridViewColumn in a DataGridView when using DataGridView.Da开发者_StackOverflowtaSource = DataSet.Table?

How can I modify the DataGridViewColumn in a DataGridView when using DataGridView.Da开发者_StackOverflowtaSource = 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.


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...


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号