如何在Windows窗体中绑定数据网格视图组合框列值? [英] How to bind the data grid view Combo box column value in windows form ?

查看:84
本文介绍了如何在Windows窗体中绑定数据网格视图组合框列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

how to i bind Data Base table values on data grid view with Combo box Column values. 

推荐答案

首先,将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
}


这篇关于如何在Windows窗体中绑定数据网格视图组合框列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆