且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Windows窗体中绑定数据网格视图组合框列值?

更新时间:2023-12-06 11:15:04

首先,将AutoGenerateColumns设置为false。明确地列出你的专栏:



First, set AutoGenerateColumns to false. Bind your columns explicitely:

private void BindTableData() {
dgv.Columns(0).DataPropertyName = "table_id"
dgv.Columns(1).DataPropertyName = "table_data1"
//.
//. other columns
//.
// before you bind combobox, you need to get the data for it.
DataTable you_table_source = GetYourComboData();
DataGridViewComboBoxColumn cb = TrYCast(dgv.Columns(your_index), DataGridViewComboBoxColumn)

cb.DataPropertyName = "your_datasource_column_name" // this is important, if you don't set this, grid datasource will not know how to set your combo
If (cb != null)
{
cb.DisplayMember = "your_display_column_name"
cb.ValueMember = "your_value_column_name"
cb.DataSource = your_table_source // you may have to call your_table_source.Copy instead of just setting the table - check if each row has its own instance of the datasource or it is set to the same reference (you will know if changing one row changes all others). In that case add .Copy as above


// AAAND You're done
}