0

希望有人可以帮助解决这个问题。我的代码中有一个无法修复的错误。我正在使用带有 DataGridView 控件的选定索引事件处理进行测试。

我创建了两列:-

第 0 列是DataGridViewTextBoxColum 第 1 列是DataGridViewComboBoxColumn

我给了我的ComboBoxColumn 一个数据源,它很小DataTable,由两个Username&列组成UserID

显示成员设置为用户名列,我将用户 ID 列设置为值成员。

我要做的只是第 0 列( ),在索引更改事件中DataGridViewTextBox填充UserID( ) 。ValueMemberDataGridViewComboBoxColumn

当我第一次加载程序时它工作正常。事件IndexChanged触发没有任何错误。但是,如果我尝试ComboBox在新行中选择 ,它会从上一行清除组合框中的值,然后抛出空引用异常。

我列出了下面的代码并突出显示了它失败的代码行:-

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadData();
        }

        public OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\riversd\Desktop\Test.accdb");

        public string sql = "SELECT * FROM [AgentList]";

        private void LoadData()
        {
            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;

            DataTable dt = AgentTable(sql, con);

            DataGridViewTextBoxColumn textbox = new DataGridViewTextBoxColumn();
            dataGridView1.Columns.Add(textbox);

            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            combo.DataSource = dt;
            combo.DisplayMember = "agentName";
            combo.ValueMember = "AgentID";


            dataGridView1.Columns.Add(combo);

        }

        public DataTable AgentTable(string SQL, OleDbConnection con)
        {
            var AgentList = new DataTable();
            var SELECTcommand = new OleDbCommand(SQL, con);
            var adaptor = new OleDbDataAdapter();

            adaptor.SelectCommand = SELECTcommand;

            con.Open();
            adaptor.SelectCommand.ExecuteNonQuery();
            adaptor.Fill(AgentList);
            con.Close();

            return AgentList;
        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridViewColumn col = dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex];
            if (col is DataGridViewComboBoxColumn)
            {
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 1 && e.Control is ComboBox)
            {

                ComboBox comboBox = e.Control as ComboBox;
                comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
            }
        }

        private void LastColumnComboSelectionChanged(object sender, EventArgs e)
        {   
            var currentcell = dataGridView1.CurrentCellAddress;
            var sendingCB = sender as DataGridViewComboBoxEditingControl;
            DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];

            // HERE IS THE LINE THAT THROES EXCEPTION WHEN MOVING FROM
            // ONE COMBOXCELL to another.
            cel.Value = sendingCB.SelectedValue.ToString();


        }


    }
4

1 回答 1

1

尝试换行:-
DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];

DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[1];

于 2013-05-07T07:50:01.107 回答