Reputation: 33
I am trying to do programming in datagridview. In which result i want is like this: when I click on first column of datagridview (which is combobox column ) values from database should be displayed in next column(textbox column).
I am getting this exception:
index was out of range. must be nonnegative
On this line:
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[0];
e.RowIndex shows -1 value.
Right now I am feeling stuck at this code. What could be the problem. Can anyone help me ?
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
dataGridView1.CellValueChanged +=
new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
dataGridView1.CurrentCellDirtyStateChanged +=
new EventHandler(dataGridView1_CurrentCellDirtyStateChanged);
}
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dataGridView1.IsCurrentCellDirty)
{
// This fires the cell value changed handler below
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[0];
// ******** e.rowindex shows -1 value.
if (cb.Value != null)
{
con.Open();
SqlCommand cmd = new SqlCommand("select rate FROM Ratemaster where Packagetype = '" + comboBox1.Text +
"' AND Tickettype ='" + ComboboxColumn.Selected + "' ", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
dataGridView1.Rows[0].Cells[1].Value = dr[0].ToString();
//dataGridView1.Rows[e.RowIndex].Cells[1].Value = "hi";
}
else
{
//txtRate.Text = "0";
}
con.Close();
}
}
Upvotes: 2
Views: 861
Reputation: 9469
@kury’s answer is correct but appears to be missing the fact that this will fire when ANY cell value is changed. In order for this to work properly… you need to also make sure the value changed WAS actually the combo box cell. So another check is necessary to make sure the COMBO BOX value was changed:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
if (e.RowIndex >= 0) {
if (e.ColumnIndex == 0) {
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[0];
//.....
//.....
}
}
}
Upvotes: 2
Reputation: 471
I did solve this problems, with a wrapper that catches and ignores the -1 range.
if(e.RowIndex >= 0)
foo();
else
bar();
And:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex >= 0)
{
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[0];
// ******** e.rowindex shows -1 value.
if (cb.Value != null)
{
con.Open();
SqlCommand cmd = new SqlCommand("select rate FROM Ratemaster where Packagetype = '" + comboBox1.Text +
"' AND Tickettype ='" + ComboboxColumn.Selected + "' ", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
dataGridView1.Rows[0].Cells[1].Value = dr[0].ToString();
//dataGridView1.Rows[e.RowIndex].Cells[1].Value = "hi";
}
else
{
//txtRate.Text = "0";
}
con.Close();
}
}
Upvotes: 2
Reputation: 883
instead of assigning 'CellValueChanged' event in dataGridView1_EditingControlShowing(), try to assign the event from design view; using properties window.
It seems that every EditingControlShowing() event the 'CellValueChanged' event is stacking up and being called again and again, which means when EditingControlShowing() trigger first time CellValueChanged is called once, next time it will be twice and go on.
Upvotes: 0