范围异常的参数未得到处理 [英] Argumentoutof range exception was unhandled
问题描述
在运行以下代码时,我收到错误,因为arugument超出范围exeption未处理.index超出范围必须是非负的&小于集合的大小。参数名称:索引
我尝试过:
while running the following code, i receive the error as arugument out of range exeption was unhandled.index was out of range must be non negative & lessthan the size of the collection.parameter name:index
What I have tried:
private void button3_Click(object sender, EventArgs e)
{
con.Open();
cmd.Connection = con;
if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 1)
{
cmd.CommandText = "delete from sam where empid="+dataGridView1.SelectedRows[0].Cells[0].Value.ToString()+" ";
cmd.ExecuteNonQuery();
//dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
MessageBox.Show("record deleted");
}
bind();
con.Close();
}
推荐答案
请快速发送解决方案
我们不能。
问题是你只是通过SelectedIndex访问行 - 我们无法运行您在相同的情况下执行代码,因此出于完全相同的原因,我们无法得到与您相同的错误。
因此请使用调试器,找出哪些部分超出范围 - 可能你没有选定的行,或者你有错误的控制,或者...我们不知道,我们无法找到。但你可以 - 只需使用调试器并查看发生了什么。
We can't.
The problem is that you are only accessing rows via the SelectedIndex - and we can't run your code under the same circumstances that you do, so we can't get the same error as you for exactly the same reasons.
So use the debugger, and find out which parts are out of range - probably you have no selected rows, or you have the wrong control, or ... we don't know, and we can't find out. But you can - just use the debugger and look at what is going on.
我认为你可能需要检查
dataGridView1.SelectedRows.Count > 0
而不是
dataGridView1.Rows.Count > 1
这篇关于范围异常的参数未得到处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!