为什么我的数据网格仅填充一行? [英] Why is my datagrid only populating one row?

查看:62
本文介绍了为什么我的数据网格仅填充一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它多次通过foreach循环使用多个值,但仅在网格中填充一行.

It's going through the foreach loop multiple times with multiple values but only populates the grid with one row.

        string textLines;
        string[] textLine;

        textLines = scannedGuid.Text;

        textLine = textLines.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);

        DataSet ds2 = null; 
        Database db2 = DatabaseFactory.CreateDatabase("CouponConnectionString");

        Database db2 = DatabaseFactory.CreateDatabase("ConnectionString");
        foreach (string s in textLine)
        {

            try
            {

                DbCommand command2 = db.GetStoredProcCommand("sel_Guid_p");
                db2.AddInParameter(command2, "@pGuid", DbType.String, s);
                ds2 = db2.ExecuteDataSet(command2);


            }

            catch (Exception ex)
            {

            }


        }

        DataGrid1.DataSource = ds2;
        DataBind();

推荐答案

我假设使用此代码

            DbCommand command2 = db.GetStoredProcCommand("sel_Guid_p");
            db2.AddInParameter(command2, "@pGuid", DbType.String, s);
            ds2 = db2.ExecuteDataSet(command2);

正在返回一行.

仅仅是因为您多次调用它,并不意味着有多个记录.您将覆盖循环执行的所有变量的值.

Just because you're calling it multiple times, doesn't mean there are multiple records. You are overwriting the value of the variable everything your loop executes.

也许您可以将数据集的结果放入一个对象(创建一个代表结果的类),然后在循环时,将每个结果对象添加到列表中,然后将该列表绑定到网格上.

Perhaps you can put the results of your dataset into an object (make a class that represents the results), then when you loop, add each result object to a list, then bind that list to the grid.

一个例子:

    List<MyCustomerClassForTheReturnedValues> values = new List<MyCustomClassForTheReturnedValues>();

    foreach (string s in textLine)
    {


            DbCommand command2 = db.GetStoredProcCommand("sel_Guid_p");
            db2.AddInParameter(command2, "@pGuid", DbType.String, s);
            MyCustomClassForTheReturnedValues x = new MyCustomClassForTheReturnedValues(db2.ExecuteDataSet(command2));
            values.Add(x);


    }


    DataGrid1.DataSource = values;
    DataGrid1.DataBind();

这篇关于为什么我的数据网格仅填充一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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