开发者

how to set value in DataGridViewComboBoxColumn from a datatable?

开发者 https://www.devze.com 2023-02-05 10:19 出处:网络
DataGridViewComboBoxColumn dgvcb = (DataGridViewComboBoxColumn)grvPackList.Columns[\"Units\"]; 开发者_运维问答Globals.G_ProductUtility G_Utility = new Globals.G_ProductUtility();
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

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?


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


you can bind the DataGridViewComboBoxColumn directly to your datasouce like

DataGridViewComboBoxColumn dgvcb = (DataGridViewComboBoxColumn)grvPackList.Columns["Units"];
dgvcb.ValueMember = "YourUnitValue"; 
dgvcb.DisplayMember = "Units";
dgvcb.DataSource = _ds.Tables[0];
0

精彩评论

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