【问题标题】:DataGridViewComboBoxColumn Strange BehaviourDataGridViewComboBoxColumn 奇怪的行为
【发布时间】:2015-08-14 19:37:33
【问题描述】:

我在 datagridview 中遇到了一些奇怪的问题。我有带有 ComboBox Column 的 datagridview。我使用了 datagridviewv_EditingControlShowing 事件,然后使用了 GridCombo_SelectedIndexChanged 事件。第一次从 Combobox 中选择一些 Disply 成员时没有问题。但在那之后它的颜色更改为黑色和蓝色。所以显示成员不显示。请查看屏幕截图了解更多详细信息。

 private void datagridview_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is TextBox)
        {
            TextBox tb = e.Control as TextBox;
            if (flgCellEdit == true)
            {
                tb.KeyPress += new KeyPressEventHandler(NumericValidation_KeyPress);
                //tb.Leave += new EventHandler(GridTextBox_LeaveEvent);
            }
            else
            {
                tb.KeyPress += new KeyPressEventHandler(NumericValidationCancel_KeyPress);
            }
        }
        if (e.Control is ComboBox)
        {
            ComboBox comboBox = e.Control as ComboBox;
            switch ((sender as DataGridView).Name)
            {
                case "dgvIPO":
                    comboBox.SelectedIndexChanged += new EventHandler(GridCombo_SelectedIndexChanged);
                    break;

            }

        }
    } private void GridCombo_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataGridView dgv = (sender as DataGridViewComboBoxEditingControl).EditingControlDataGridView as DataGridView;
        DataGridViewRow dgvr = dgv.Rows[(sender as DataGridViewComboBoxEditingControl).EditingControlRowIndex];
        switch (dgv.Name)
        {

             case "dgvIPO":
                if (dgvr.Cells[5].EditedFormattedValue.ToString() == "N" && dgvr.Cells[6].EditedFormattedValue.ToString() == "N" && dgvr.Cells[7].EditedFormattedValue.ToString() == "N" && dgvr.Cells[8].EditedFormattedValue.ToString() == "N" && dgvr.Cells[9].EditedFormattedValue.ToString() == "N")
                {
                    dgvr.Cells[10].Value = "Complies";
                }
                else
                {
                    dgvr.Cells[10].Value = "Non Complies";
                }
                txtAcceptedQtySt1.Text = dgvIPO.Rows.Cast<DataGridViewRow>().Where(r => r.Cells[10].EditedFormattedValue.ToString() == "Complies").Count().ToString();
                break;
        }
    }`

【问题讨论】:

标签: c# winforms datagridview datagridcomboboxcolumn


【解决方案1】:

我通过避免 EditingControlShowing 事件并使用 CurrentCellDirtyStateChanged 事件立即提交值,然后使用带有 Value 而不是 EditedFormattedValue 的 CellValueChanged 事件来解决(未解决)问题。请参阅 Severun 对 this question 的回答,它帮助我解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2015-07-20
    • 2010-10-03
    • 2021-07-12
    • 2013-10-04
    相关资源
    最近更新 更多