如何在CellContentClick事件中手动选择DataGridviewComboBoxCell值



嗨,程序员们,事实上,我在DataGridvIew中有一个DataGridViewComboBoxCell,如果触发CellContentClick事件时条件为true,我需要更改DataGridViewcomoBox值。我的代码是这样的:

    private void gridviewholiday_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        int row = e.RowIndex;
        int colo = e.ColumnIndex;

        /*=============== To Show The Details  =====================*/
        if (e.ColumnIndex == 4)
        {
            if (Convert.ToBoolean(gridviewholiday.Rows[e.RowIndex].Cells[0].Value))
            {
                if (Type == "CUS")
                {
                    Type = test.colType;
                    if (Type == "NO")
                    {

                        ComboBox combo = (ComboBox)sender;
                        combo.SelectedIndex = 0;
                    }
                }
    }

但它在将DataGridView强制转换为组合框时出错。

请帮帮我。

朋友们好!

我得到了答案,我手动选择了DataGridviewComboBoxCell。

private void gridviewholiday_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
    int row = e.RowIndex;
    int colo = e.ColumnIndex;

    /*=============== To Show The Details  =====================*/
    if (e.ColumnIndex == 4)
    {
        if (Convert.ToBoolean(gridviewholiday.Rows[e.RowIndex].Cells[0].Value))
        {
            if (Type == "CUS")
            {
                Type = test.colType;
                if (Type == "NO")
                {
                     /*===== set the selected value of comboboxCellItems   ==========*/
                      gridviewholiday.Rows[e.RowIndex].Cells["colType"].Value="ALL"

                }
            }
}

我的问题终于解决了。

试试这个

Private Sub dgvMain_CellMouseEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMain.CellMouseEnter
        Dim dgv As DataGridView
        dgv = DirectCast(sender, DataGridView)
        If dgv IsNot Nothing Then
            Dim cmb As DataGridViewComboBoxCell 
            cmb = DirectCast(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewComboBoxCell)
            If cmb IsNot Nothing Then
                cmb.Selected = True
            End If
        End If
    End Sub

相关内容

  • 没有找到相关文章

最新更新