2012-11-23 79 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];