如何在Gridview中显示多个记录 [英] How Do I Display Multiple Records In Gridview

查看:74
本文介绍了如何在Gridview中显示多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

str = "select SrNo,Description,Qty,Rate,Amount from billobservation where BillNo =" + txtbillno.Text;
adap = null;
adap = new SqlDataAdapter(str, sqlcon);
DataSet ds1 = new DataSet();
adap.Fill(ds1, "billobservation1");
dgv.DataSource = null;
dgv.Rows.Clear();

for (int i = 0; i <= ds1.Tables["billobservation1"].Rows.Count - 1; i++)
{
    for (int j = 0; j <= dgv.ColumnCount - 1; j++)
    {
        
        dgv.Rows[i].Cells[j].Value = ds1.Tables["billobservation1"].Rows[i][j];
        
    }
}




Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index



(此错误显示)


(this error display)

推荐答案

更改

Change
dgv.DataSource = null;
dgv.Rows.Clear();

for (int i = 0; i <= ds1.Tables["billobservation1"].Rows.Count - 1; i++)
{
    for (int j = 0; j <= dgv.ColumnCount - 1; j++)
    {

        dgv.Rows[i].Cells[j].Value = ds1.Tables["billobservation1"].Rows[i][j];

    }
}





to



to

dgv.DataSource = ds1;
dgv.DataBind();


Quote:

dgv.Rows [i] .Cells [j] .Value = ds1.Tables [billobservation1]。行[i] [j];

dgv.Rows[i].Cells[j].Value = ds1.Tables["billobservation1"].Rows[i][j];

此时此异常很有可能发生。



所以,做一件事。在这里放一个调试器。并运行您的代码。在立即窗口中查看数据是否正确无误。您将找到发生此异常的位置,并在此时标记 i j 的值。



还有一件事......

There is a good chance for this exception to occur at this point.

So, do one thing. Put a debugger here. and run your code. See if data are correctly coming correctly or not in Immediate window. You will find the point where this exception occurs and mark the values of i and j at that point.

One more thing is...

for (int i = 0; i <= ds1.Tables["billobservation1"].Rows.Count - 1; i++)
{
    for (int j = 0; j <= dgv.ColumnCount - 1; j++)



在这里标记for循环中的条件。



For第一个 For Loop ,您正在检查行数 DataSet ds1 和第二个 For Loop ,您正在检查中的列DataGridView dgv



所以,可能有点,哪里会有计数不匹配会导致索引,但代码<$ c不存在$ c> dgv.Rows [i] .Cells [j] .Value = ds1.Tables [billobservation1]。行[i] [j];


Mark the conditions inside for loops here.

For the first For Loop, you are checking Rows Count from DataSet ds1 and for second For Loop, you are checking Columns from DataGridView dgv.

So, there might be point, where there will be a mismatch in Rows and Columns count which would lead to a index which does not exist for the code dgv.Rows[i].Cells[j].Value = ds1.Tables["billobservation1"].Rows[i][j];


这篇关于如何在Gridview中显示多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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