我想在datagridview中编程。我想要的结果是这样的:当我点击datagridview(这是combobox列)的第一列时,数据库中的值应显示在下一列(textbox列)中。在formload上得到datagridview错误:索引超出范围异常
我得到这个异常:
指数超出范围。必须是非负
在此行中:
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[0];
e.RowIndex显示值-1。
现在我感觉卡在这段代码中了。可能是什么问题呢。谁能帮我 ?
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();
}
}
感谢您的建议朋友。但是我没有得到foo()和bar()的含义。由于我是编程新手,我完全不理解。你能详细说明你的答案吗? –
嗨,富和酒吧,特别是连字foobar是任何代码的常见占位符,你可以想象。我想表达一下,你应该用你自己的代码对if子句作出反应,并忽略其他路径。所以你会这样做: – kurdy
@ V.Vaibhav很多时候,程序员会用'foo'和'bar'或'foobar'来演示一个例子。上面基本上是说'if(e.RowIndex> = 0)'然后做一些事情 - 其中'foo();'是什么(不管你想要你的代码做什么,然后用它替换'foo()')。代码的else部分也是如此。 –