C# 找不到;选择索引“;Visual Studio 2010中DataGridViewComboxColumn的属性
我不熟悉windows窗体应用程序开发 我正在使用可编辑的网格视图进行数据输入 网格视图中的一个字段的类型为ComboBoxColumn。我正在用代码填充数据 我的问题是,如果数据项计数大于0,则应自动选择第一项 我的代码来自C# 找不到;选择索引“;Visual Studio 2010中DataGridViewComboxColumn的属性,c#,winforms,visual-studio-2010,selectedindex,datagridcomboboxcolumn,C#,Winforms,Visual Studio 2010,Selectedindex,Datagridcomboboxcolumn,我不熟悉windows窗体应用程序开发 我正在使用可编辑的网格视图进行数据输入 网格视图中的一个字段的类型为ComboBoxColumn。我正在用代码填充数据 我的问题是,如果数据项计数大于0,则应自动选择第一项 我的代码来自Page\u Load(): private void Form1_Load(object sender, EventArgs e) { cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Dat
Page\u 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.
}
请帮忙解决这个问题
提前感谢。类
DataGridViewComboxCell
没有这些属性。查看
我们尝试了不同的方法。在您的代码中,它看起来像这样:
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;
}
}
中巴。选择编辑。你在找这个吗?是的,但我找不到
cmb.SelectedItem
也找不到cmb.SelectedIndex
@FaisalHafeezcmb.Value或cmb.DisplayMember.@RiyazKalva你试过将它强制转换到一个常规的组合框:(组合框)(dataGridView1.Rows[e.RowIndex].Cells[1])代码>?@RiyazKalva正常。尝试处理DataGridView.EditingControlShowing
事件,如建议的那样,先生,我按照您的答案进行了尝试,但是dataGridView1\u EditingControlShowing()
中出现错误,无法将类型为DataGridViewTextBoxEditingControl
的对象强制转换为类型ComboBox
aah yeah-sry,我的强制转换错误。并非每个单元格都包含组合框。现在它将只返回null,而不是抛出错误。试试看。@RiyazKalva I'v更新了answe。问题还在吗?先生,我试过你更新的答案,程序没有错误,但记录不是从数据集中提取的。我调试了程序,结果显示控件没有进入dataGridView1\u CellBeginEdit()
@JensKloster