本文介绍了我如何在C#中的gridview中填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写这段代码: -
i write this code :-
void FillComboboxCarPlace()
{
try
{
dSet = new DataSet();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
s = "select Move_id , Move_Place from Car_Move_Place";
sCommand = new SqlCommand(s, con);
sdAdapter = new SqlDataAdapter();
sdAdapter.SelectCommand = sCommand;
sdAdapter.Fill(dSet);
DataRow dr = dSet.Tables[0].NewRow();
dr.ItemArray = new object[2] { 0, " ---Select--- " };
dSet.Tables[0].Rows.InsertAt(dr, 0);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//DataGridViewComboBoxCell ContactCombo = (DataGridViewComboBoxCell)(row.Cells["Car_Move_Place"]);
ComboMovePlace.ValueMember = "Move_id";
ComboMovePlace.DisplayMember = "Move_Place";
ComboMovePlace.DataSource = dSet.Tables[0];
}
//ComboMovePlace.ValueMember = "Move_id";
//ComboMovePlace.DisplayMember = "Move_Place";
//ComboMovePlace.DataSource = dSet.Tables[0];
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
catch
{
return;
}
}
我的尝试:
i尝试编写这段代码,但它不正确
What I have tried:
i try write this code but it's uncorrect
推荐答案
int index = 0; // index of the combox box column ( zero based index )
foreach (DataGridViewRow row in dataGridView1.Rows)
{
var cbxMove = row.Cells[index] as DataGridViewComboBoxCell;
cbxMove.ValueMember = "Move_id";
cbxMove.DisplayMember = "Move_Place";
cbxMove.DataSource = dSet.Tables[0];
}
这篇关于我如何在C#中的gridview中填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!