2012-11-23 117 views
0

希望有人可以帮助解决这个问题。我的代码中有一个我无法修复的错误。我正在使用DataGridView控件使用Selected Index Event处理测试。DataGridViewComboBox索引更改事件故障

我已经创建了两个列: -

列0是DataGridViewTextBoxColum 第1列是DataGridViewComboBoxColumn

我已经给了我ComboBox列一个数据源是小DataTable由两列这是Username的& UserID

将显示成员设置为用户名列,并将UserID列设置为ValueMember。

所有我希望做的是对的DataGridViewComboBoxColumn指数Changed事件列0(DataGridViewTextBox),填充与UserIDValueMember)。

当我第一次加载程序时它工作正常。 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(); 


     } 


    } 

回答

1

尝试改变路线: -
DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];

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