Hope someone can help on this one. I have a bug in my code that I cant fix. I'm testing out using Selected Index Event handling with DataGridView control.
I have created two columns:-
Column 0 is a DataGridViewTextBoxColum
Column 1 is a DataGridViewComboBoxColumn
I have gave my ComboBox
Column a Datasource that is a small DataTable
consisting of two Columns which are Username
& UserID
.
The Display Member is set to the Username Column and I have the UserID column set as the ValueMember.
All I'm looking to do is Column 0(DataGridViewTextBox
), populate with the UserID
(ValueMember
) on the Index Changed event for the DataGridViewComboBoxColumn
.
It works fine when I first load the program. The IndexChanged
Event Fires without any error. But if I try to select the ComboBox
in the new row, it is clearing the value in combobox from the previous row, and then throwing a Null Reference Exception.
I have listed the Code Below and Highlighted the line of the code that it fails at :-
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();
}
}