我遍历数组和如何填充DataGrid? [英] How can I loop through an array and populate a datagrid?

查看:135
本文介绍了我遍历数组和如何填充DataGrid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的情况:

我有我读每一行到一个数组多行文本框。我再通过数组循环和一个内循环发送每个值到存储的过程之一,并获得一个状态回来告诉它是否有效。

问题是我相信我重写数据变量,只用填充的最后一个值检索到数据网格。有没有更好的方式来实现什么即时试图做的?如果是这样,请解释。

 保护无效submitButton_Click(对象发件人,EventArgs的发送)
    {
        字符串文本行;
        字符串[] TEXTLINE;        文本行扫描= code.Text;        TEXTLINE = textLines.Split(Environment.NewLine.ToArray(),StringSplitOptions.RemoveEmptyEntries);        数据集DS = NULL;
        数据库DB = DatabaseFactory.CreateDatabase(ConnectionString的);
        数据集DS2 = NULL;
        数据库DB2 = DatabaseFactory.CreateDatabase(的ConnectionString);        的foreach(在TEXTLINE字符串s)
        {
            尝试
            {
                的DbCommand命令2 = db.GetStoredProcCommand(sel_InfoByID_p);
                db2.AddInParameter(命令2@pGuid,DbType.String,S);
                DS2 = db2.ExecuteDataSet(命令2);
                DataGrid1.DataSource = DS2;
                的DataBind();             }            赶上(异常前)
            {            }
        }    }


解决方案

创建循环一个DataTable外,并在您添加返回行。

  DataTable的DT =新的DataTable();

/// ...添加称为列在这里。

内环路行添加到表中。循环绑定表一次后。

 保护无效submitButton_Click(对象发件人,EventArgs的发送)
    {
        字符串文本行;
        字符串[] TEXTLINE;        文本行扫描= code.Text;        TEXTLINE = textLines.Split(Environment.NewLine.ToArray(),StringSplitOptions.RemoveEmptyEntries);        数据集DS = NULL;
        数据库DB = DatabaseFactory.CreateDatabase(ConnectionString的);
        数据集DS2 = NULL;
        数据库DB2 = DatabaseFactory.CreateDatabase(的ConnectionString);        DataTable的DT =新的DataTable();
        这里///...Add称为列
        的foreach(在TEXTLINE字符串s)
        {
            尝试
            {
                的DbCommand命令2 = db.GetStoredProcCommand(sel_InfoByID_p);
                db2.AddInParameter(命令2@pGuid,DbType.String,S);
                的DataRow myNewRow = db2.ExecuteDataSet(命令2).tables [0] .rows [0];
                dt.Rows.Add(myNewRow);             }            赶上(异常前)
            {            }
        }
               DataGrid1.DataSource = DT;
                的DataBind();
    }

Here's the scenario:

I have a multiline text box that I'm reading each line into an array. I'm then looping through that array and sending each value to the stored proc one by one inside the loop and getting a status returned to tell whether it's valid or not.

The issue is I believe I'm overwriting the dataset variable and only populating the datagrid with the last value I retrieve. Is there a better way to achieve what im trying to do? If so, please explain.

protected void submitButton_Click(object sender, EventArgs e)
    {
        string textLines;
        string[] textLine;

        textLines = scannedCode.Text;

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

        DataSet ds = null;
        Database db = DatabaseFactory.CreateDatabase("ConnectionString");
        DataSet ds2 = null; 
        Database db2 = DatabaseFactory.CreateDatabase("ConnectionString");

        foreach (string s in textLine)
        {
            try
            {
                DbCommand command2 = db.GetStoredProcCommand("sel_InfoByID_p");
                db2.AddInParameter(command2, "@pGuid", DbType.String, s);
                ds2 = db2.ExecuteDataSet(command2);
                DataGrid1.DataSource = ds2;
                DataBind();

             }

            catch (Exception ex)
            {

            }
        }



    }

解决方案

Create a DataTable outside and in your for loop add your returned row .

DataTable dt = new DataTable();

///...Add known columns here

Inside for loop add rows to table . After for loop bind table at once.

protected void submitButton_Click(object sender, EventArgs e)
    {
        string textLines;
        string[] textLine;

        textLines = scannedCode.Text;

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

        DataSet ds = null;
        Database db = DatabaseFactory.CreateDatabase("ConnectionString");
        DataSet ds2 = null; 
        Database db2 = DatabaseFactory.CreateDatabase("ConnectionString");

        DataTable dt = new DataTable();
        ///...Add known columns here
        foreach (string s in textLine)
        {
            try
            {
                DbCommand command2 = db.GetStoredProcCommand("sel_InfoByID_p");
                db2.AddInParameter(command2, "@pGuid", DbType.String, s);
                DataRow myNewRow = db2.ExecuteDataSet(command2).tables[0].rows[0];
                dt.Rows.Add(myNewRow);

             }

            catch (Exception ex)
            {

            }
        }


               DataGrid1.DataSource = dt;
                DataBind();


    }

这篇关于我遍历数组和如何填充DataGrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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