如何确保用户在Datagridview中选择不同的组合框选项

时间:2014-05-22 08:45:35

标签: c# winforms datagridview

我有一个名为ColumnLocationDemo的组合框列的gridview。我想确保用户每次从comobox中选择不同的选项。我正在尝试这个代码消息框出现,但不知道如何更改columnLocationDemo的索引?此gvCombobox中没有selectedIndex属性

private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            string str1 = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            for (int i = 0; i < dataGridView1.Rows.Count - 2; i++)
            {
                string str = dataGridView1.Rows[i].Cells[1].Value.ToString();
                if (str==str1)
                {
                    MessageBox.Show("same occur");
                    ColumnLocationDemo
                }
            }


        }

3 个答案:

答案 0 :(得分:1)

这个怎么样...
不要使用currentRow。只需将新值与已选择的值进行比较。

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            for (int j = i + 1; j < dataGridView1.Rows.Count - 1;j++ )
            {
                if (dataGridView1.Rows[i].Cells[0].Value.ToString() == dataGridView1.Rows[j].Cells[0].Value.ToString())
                {
                    dataGridView1.Rows[j].Cells[0].Value = "";
                }
            }

        }

答案 1 :(得分:0)

columnLocationDemo.SelectedIndex = 0;

如果这是您的comboBox的名称。您也可以选择任何索引。

答案 2 :(得分:-1)

试试这个:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    //your work;
}
相关问题