将 DataSet 与 DataGridView 结合使用并修改 DataGridViewColumn
使用 DataGridView.DataSource = DataSet.Table 时如何修改 DataGridView 中的 DataGridViewColumn?
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?
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能修改列。
如果您想要 ComboBoxColumn,则需要先将其添加到 DataGridView。在绑定之前添加组合框列并设置其 DataPropertyName,以便网格将正确的数据绑定到该列,而不是创建新的数据。
另一种方法是,如果可以的话,使用强类型 DataSet,因为它会自动为您的 bool 列创建 CheckBoxColumns 等...
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...
使用数据网格的 AutoGenerateColumn 事件。这将为您提供正在创建的列的句柄,并且您可以根据需要更改该列。下面的代码直接来自VS2010帮助文件:
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: