我是Windows窗体应用程序开发的新手。
我正在使用可编辑的网格视图进行数据输入。
网格视图中的一个字段是ComboBoxColumn类型。我在代码中填充数据。
我的问题是,如果数据项计数大于0,则应自动选择第一项。
Page_Load()
的代码是:
private void Form1_Load(object sender, EventArgs e)
{
cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Study\sem 6\Practice\WindowsFormsApplication1\Practice.accdb");
cn.Open();
cmd = new OleDbCommand("Select * from Grade", cn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
cn.Close();
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridViewComboBoxCell cmb = (DataGridViewComboBoxCell)(dataGridView1.Rows[e.RowIndex].Cells[1]);
cmb.DataSource = ds.Tables[0];
cmb.DisplayMember = "Grd";
cmb.ValueMember = "ID";
if(cmb.Items.Count > 0)
// Here I am not finding the the combo box's SelectedIndex Property.
}
请帮忙解决这个问题。
提前致谢。
答案 0 :(得分:0)
班级DataGridViewComboBoxCell
没有这些属性。查看documentaion
Others尝试了不同的方法。在你的代码中,它看起来像是这样的想法:
private ComboBox _chashedComboBox;
private void dataGridView1_EditingControlShowing()
{
_chashedComboBox = e.Control as ComboBox;
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
var cmb = _chashedComboBox;
if(cmb != null)
{
cmb.DataSource = ds.Tables[0];
cmb.DisplayMember = "Grd";
cmb.ValueMember = "ID";
if(cmb.Items.Count > 0)
cmb.SelectedIndex = 0;
}
}