将DataGridViewComboBox设置为与现有DataGridView列相同 [英] Set DataGridViewComboBox default equal to existing DataGridView column

查看:185
本文介绍了将DataGridViewComboBox设置为与现有DataGridView列相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将$ code> DataGridViewComboBox 添加到绑定的 DataGridView grdBOOK ), DataGridViewComboBox 将替换列3以允许用户选择。我正在努力设置默认值为$ 3的数据类型DataGridViewComboBox 的值,因此如果值正确,则不需要用户选择。

I have added a DataGridViewComboBox to a bound DataGridView (grdBOOK), the DataGridViewComboBox will replace column 3 to allow for user selection. I'm struggling to set the default of the DataGridViewComboBox equal to the value of column 3 so user selection is not required if the value is correct.

我把代码从 net ,但是我收到一个错误:

I pulled the code below from the net, but I get an error:


DataGridViewComboBoxCell值无效。

DataGridViewComboBoxCell value is not valid.

我以为一个 ComboBox 单元格可以被视为普通的 DataGridView 单元格,但是(见下面的代码)当将字符串添加到 ComboBox 列时会生成错误我已经拖网了几天,但没有任何工作,有什么建议吗?

I thought a ComboBox cell could be treated as a normal DataGridView cell, but (see code below) an error is generated when a string is added to the ComboBox column? I've trawled the net and SO for a few days but nothing works, any suggestions please?

    public void BOOK_COMBO2()       
        {
            DataGridViewComboBoxCell cb_cell = new DataGridViewComboBoxCell();
            DataGridViewComboBoxColumn cb_col = new DataGridViewComboBoxColumn();

            // Contract field
            cb_col.Items.AddRange("YEARLY", "MONTHLY", "");
            cb_col.FlatStyle = FlatStyle.Flat;
            cb_col.HeaderText = "newCONTRACT";
            cb_col.Width = 50;
            cb_col.ValueType = typeof(string);

            // Add ComboBox and test
            grdBOOK.Columns.Insert(5, cb_col);
            grdBOOK.Rows[14].Cells[4].Value = "zzz";        // No error adding string to normal dgv column
            grdBOOK.Rows[14].Cells[5].Value = "xxx";        // Error adding string to dgvcombobx column

            //copy old values to new combobox and set as default
            foreach (DataGridViewRow item in grdBOOK.Rows)
            {
                item.Cells[5].Value = item.Cells[3].Value;      
            }
            //hide original column
            grdBOOK.Columns[3].Visible = false;
        }


推荐答案

  private void dataGridView1_DataError(object sender,
            DataGridViewDataErrorEventArgs e)
        {
            // If the data source raises an exception when a cell value is 
            // commited, display an error message.
            if (e.Exception != null &&
                e.Context == DataGridViewDataErrorContexts.Commit)
            {
                MessageBox.Show("");
            }
        }




private void Form1_Load(object sender, EventArgs e)
    { dataGridView1.DataError +=
                dataGridView1_DataError;}

这篇关于将DataGridViewComboBox设置为与现有DataGridView列相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆