索引超出范围异常。 [英] Index out of range exception.

查看:97
本文介绍了索引超出范围异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我运行查询,它给我7行的数据,但我运行循环打印dgv中的7行。打印只有一行,当尝试打印下一行时,错误显示在dgv的插入语句处。



这里是我的代码供参考,



I have one form in which i run query which gives me data of 7 rows but while i running loop to print 7 rows in dgv . only one row printed and when trying to print next row above error shows at insertion statement of dgv.

here is my code for reference,

private void btnsearch_Click(object sender, EventArgs e)
{
   try
   {
     DateTime frmdate = Convert.ToDateTime(dtpfrom.Text);
     DateTime todate = Convert.ToDateTime(dtpto.Text);
     string datetimefrm = frmdate.ToString("yyyy-MM-dd 00:00:00.000");
     string datetimeto = todate.ToString("yyyy-MM-dd 00:00:00.000");

    if (ckaprov.Checked == true)
    {
       cmd = new SqlCommand();
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.CommandText = "Proc_GetIndentBetweenDate";
       cmd.Parameters.Add(new SqlParameter("fromDate", datetimefrm));
       cmd.Parameters.Add(new SqlParameter("@toDate", datetimeto));
       cmd.Connection = con;
       table = new DataTable();
       adapter = new SqlDataAdapter(cmd);
       adapter.Fill(table);
       int count = table.Columns.Count;

    if (table.Rows.Count > 0)
    {
       foreach (DataRow rw in table.Rows)
       {
         string no = rw["Indent No"].ToString();
         string date = rw["Indent Date"].ToString();
         string frm = rw["From Store"].ToString();
         string to = rw["To Store"].ToString();
         string status = rw["Indent Status"].ToString();
         string emergency = rw["Emergency Indent"].ToString();

        for (int i = 0; i <= table.Rows.Count - 1; i++)
        {
           dataGridView1.Rows[i].Cells[1].Value = no;
           dataGridView1.Rows[i].Cells[2].Value = date;
           dataGridView1.Rows[i].Cells[3].Value = frm;
           dataGridView1.Rows[i].Cells[4].Value = to;
           dataGridView1.Rows[i].Cells[5].Value = status;
           dataGridView1.Rows[i].Cells[6].Value = emergency;
           dataGridView1.Rows[i].Cells[7].Value = "Send";
        }
         // dataGridView1.Rows.Add();
    }
  }
else { MessageBox.Show("No indents present between these dates."); }
}
else
{
MessageBox.Show("No open indents found , Please check Approved.");
}
}
catch { }
}

推荐答案

这不是行。

这是单元格。



C#数组索引基于零,所以这些可能是:

It's not the rows.
It's the cells.

C# array indexes are zero based, so it's likely that these:
for (int i = 0; i <= table.Rows.Count - 1; i++)
{
   dataGridView1.Rows[i].Cells[1].Value = no;
   dataGridView1.Rows[i].Cells[2].Value = date;
   dataGridView1.Rows[i].Cells[3].Value = frm;
   dataGridView1.Rows[i].Cells[4].Value = to;
   dataGridView1.Rows[i].Cells[5].Value = status;
   dataGridView1.Rows[i].Cells[6].Value = emergency;
   dataGridView1.Rows[i].Cells[7].Value = "Send";
}

应该是:

Should be these:

for (int i = 0; i <= table.Rows.Count - 1; i++)
{
   dataGridView1.Rows[i].Cells[0].Value = no;
   dataGridView1.Rows[i].Cells[1].Value = date;
   dataGridView1.Rows[i].Cells[2].Value = frm;
   dataGridView1.Rows[i].Cells[3].Value = to;
   dataGridView1.Rows[i].Cells[4].Value = status;
   dataGridView1.Rows[i].Cells[5].Value = emergency;
   dataGridView1.Rows[i].Cells[6].Value = "Send";
}



[edit]

或者,可能是你的DGV中没有足够的行 - 你必须使用Rows.Add方法而不是假设存在一行。

[/ edit]


[edit]
Alternatively, it could be that you just don't have enough rows in your DGV - you would have to use the Rows.Add method instead of assuming a row exists.
[/edit]


为什么不使用DataTable作为DataGridView的DataSource ?

直接或通过BindingSource。

它会使你的代码更简单。

Why not use the DataTable as the DataSource of the DataGridView?
Either directly or via a BindingSource.
It will make your code a bit simpler.
private void btnsearch_Click(object sender, EventArgs e)
{
    try
    {
        DateTime frmdate = Convert.ToDateTime(dtpfrom.Text);
        DateTime todate = Convert.ToDateTime(dtpto.Text);
        string datetimefrm = frmdate.ToString("yyyy-MM-dd 00:00:00.000");
        string datetimeto = todate.ToString("yyyy-MM-dd 00:00:00.000");
        
        if (ckaprov.Checked == true)
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Proc_GetIndentBetweenDate";
            cmd.Parameters.Add(new SqlParameter("fromDate", datetimefrm));
            cmd.Parameters.Add(new SqlParameter("@toDate", datetimeto));
            cmd.Connection = con;
            table = new DataTable();
            adapter = new SqlDataAdapter(cmd);
            adapter.Fill(table);

            dataGridView1.DataSource = table.DefaultView;
        }
        else
        {
            MessageBox.Show("No open indents found , Please check Approved.");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}



此外,隐藏异常通常是一个坏主意,尤其是在调试期间。


Also, hiding an exception is usually a bad idea, especially during debugging.


这篇关于索引超出范围异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆