如何遍历数组并填充数据网格?

时间:2011-07-12 19:42:16

标签: c# asp.net loops

以下是该方案:

我有一个多行文本框,我正在将每行读入一个数组。然后,我循环遍历该数组,并在循环内逐个将每个值发送到存储过程,并返回一个状态,以判断它是否有效。

问题是我相信我正在覆盖数据集变量,只用我检索的最后一个值填充数据网格。有没有更好的方法来实现我想要做的事情?如果是,请解释。

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)
            {

            }
        }



    }

4 个答案:

答案 0 :(得分:1)

在外部创建一个DataTable,在for循环中添加返回的行。

DataTable dt = new DataTable();

/// ...在此处添加已知列

在for循环中向表中添加行。在for循环绑定表之后立即。

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();


    }

答案 1 :(得分:1)

首先,可以使用DataSet.Merge方法合并两个dataSet。这意味着,您应该在循环外部创建一个新的DataSet,然后将其合并到存储过程创建的DataSet中。另一种解决方案是使用Table的ImportRow方法将行从一个DataSet复制到另一个DataSet。后一种解决方案对我来说更好。以下是一些示例代码:

DataTable dt = new DataTable;
...  

ds2 = db2.ExecuteDataSet(command2);
for(int i = 0; i < ds2.Tables[0].Rows.Count; i ++)
dt.ImportRow(ds2.Tables[0].Rows[i]);

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

答案 2 :(得分:0)

试试这个

DataTable obj_Tb=new DataTable();
  obj_Tb.Columns.Add("ColumnName");
   .  //Add Columns as your requirement
   .
   .

foreach (string s in textLine)
        {
            try
            {
                DataRow objrow=obj_Tb.NewRow();
                DbCommand command2 = db.GetStoredProcCommand("sel_InfoByID_p");
                db2.AddInParameter(command2, "@pGuid", DbType.String, s);
                ds2= db2.ExecuteDataSet(command2);
          objrow["ColumnName"]=ds2.Table[0].Rows[RowNumber]["ColumnName"].tostring();
                //Add Values to all columns as requirement
                obj_Tb.Rows.Add(objrow);

             }

            catch (Exception ex)
            {

            }
}
              DataGrid1.DataSource = obj_Tb;
              DataGrid1.DataBind();

答案 3 :(得分:0)

实际上你的问题是你在EACH迭代中绑定网格,否则代码没有错。所以而不是:

    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)
        {
        }
    }

这样做:

    foreach (string s in textLine)
    {
        try
        {
            DbCommand command2 = db.GetStoredProcCommand("sel_InfoByID_p");
            db2.AddInParameter(command2, "@pGuid", DbType.String, s);
            ds2 = db2.ExecuteDataSet(command2);
         }
        catch (Exception ex)
        {
        }
    }

    DataGrid1.DataSource = ds2;
    DataBind();

但是,由于你正在循环使用相同的功能,为了优化我建议:

    SqlConnection connection = new SqlConnection(ConnectionString);
    SqlCommand command = new SqlCommand("MyStoredProcName", connection);
    command.CommandType = CommandType.StoredProcedure;

    SqlParameter submitParam = new SqlParameter("@pGuid", SqlDbType.String);

    //use SqlDbType.Bit if  you are returning true/false.      
    SqlParameter returParameter = new SqlParameter("@ReturnedParam", SqlDbType.String);  
    returParameter.Direction = ParameterDirection.Output;

    connection.Open();

    foreach (string s in textLine)
    {
            returnString.Value = s;            
            command.Parameters.Clear(); 
            command.Parameters.Add(submitParam); 
            command.Parameters.Add(returParameter);         

            try
            {                
                    command.ExecuteNonQuery();
                    store the returned string in DataTable, BindingList<string> or any, but this is how to retrieve it:
                Convert.ToString(Command.Parameters["@ReturnedParam"].Value, CultureInfo.CurrentCulture)
            }
            catch (Exception ex)
            {
                ;;
            }
    }

    connection.Close();

    Do the grid binding here