windows 如何从数据表中设置 DataGridViewComboBoxColumn 中的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4744384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set value in DataGridViewComboBoxColumn from a datatable?
提问by Nighil
DataGridViewComboBoxColumn dgvcb = (DataGridViewComboBoxColumn)grvPackList.Columns["Units"];
Globals.G_ProductUtility G_Utility = new Globals.G_ProductUtility();
dgvcb.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
G_Utility.addUnittoComboDGV(dgvcb);
DataSet _ds = iRawMaterialsRequest.Select();
grvPackList.DataSource = _ds.Tables[0];
the problem is that the DataGridViewComboBoxColumn in the datagrid is not selected with the value in table how is it possilbe to set value of DataGridViewComboBoxColumn from datasoure
问题是数据网格中的 DataGridViewComboBoxColumn 没有选择表中的值如何从数据源设置 DataGridViewComboBoxColumn 的值
int i=0;
foreach (DataRow dgvr in _ds.Tables[0].Rows )
{
grvPackList.Rows[i].Cells["Units"].Value = dgvr["Units"].ToString();
i++;
}
this code is working but is there any solution with out using loops?
这段代码正在运行,但有没有不使用循环的解决方案?
回答by Nighil
int i=0;
foreach (DataRow dgvr in _ds.Tables[0].Rows )
{
grvPackList.Rows[i].Cells["Units"].Value = dgvr["Units"].ToString();
i++;
}
When i Tried this it worked fine
当我尝试这个时它工作正常
回答by Binil
you can bind the DataGridViewComboBoxColumn directly to your datasouce like
您可以将 DataGridViewComboBoxColumn 直接绑定到您的数据源,例如
DataGridViewComboBoxColumn dgvcb = (DataGridViewComboBoxColumn)grvPackList.Columns["Units"];
dgvcb.ValueMember = "YourUnitValue";
dgvcb.DisplayMember = "Units";
dgvcb.DataSource = _ds.Tables[0];