数据网格查看数据库

时间:2015-01-29 10:02:28

标签: c#

private void buttonsubmit_Click_1(object sender, EventArgs e)  
{
    String column1 =(dataGridView1.Rows[0].Cells["RegNo"].Value.ToString());

    CLib.idUpdateTable("Attendence", "Reg_No='"+column1+"'", "No_Present", 
    dataGridView1.SelectedRows[0].Cells[4].ToString(), "No_Days", 
    dataGridView1.SelectedRows[0].Cells[5].ToString(),"Present_percentage",
    dataGridView1.SelectedRows[0].Cells[6].ToString(), "Month" , 
    dataGridView1.SelectedRows[0].Cells[3].ToString());

    IdLoad();     
}

我收到的错误是这样的:Index was out of range. Must //be non-negative and less than the size of the collection. Parameter name: index

1 个答案:

答案 0 :(得分:0)

如果您提供错误的确切位置,评论/指导会更容易。

我怀疑,错误可能是由于用于读取网格条目的索引值。下面的代码可能会帮助您忽略异常,我强烈建议放置调试点并确定DataGrid包含的值。使用列名(如果可能)而不是索引来读取DataGrid值。

if (dataGridView1.Rows.Count > 0 && dataGridView1.Rows[0].Cells.Count > 6)
{
     CLib.idUpdateTable("Attendence", "Reg_No='"+column1+"'","No_Present",
        dataGridView1.SelectedRows[0].Cells[4].ToString(), "No_Days",
        dataGridView1.SelectedRows[0].Cells[5].ToString(), "Present_percentage",
        dataGridView1.SelectedRows[0].Cells[6].ToString(), "Month",
        dataGridView1.SelectedRows[0].Cells[3].ToString());
}