繁体   English   中英

在Visual Studio 2010中找不到DataGridViewComboBoxColumn的“ SelectedIndex”属性

[英]Not able to find “SelectedIndex” property of DataGridViewComboBoxColumn in Visual Studio 2010

我是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.
}

请帮助解决此问题。

提前致谢。

DataGridViewComboBoxCell没有这些属性。 文献

其他人尝试了另一种方法。 在您的代码中,它看起来像这样:

    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;
             }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM