如何循环遍历数组并填充数据网格?
本文关键字:填充 数据 数据网 网格 数组 遍历 何循环 循环 | 更新日期: 2023-09-27 18:02:06
场景如下:
我有一个多行文本框,我正在将每一行读取到数组中。然后循环遍历该数组,并在循环中将每个值逐个发送给存储过程,并返回一个状态来判断它是否有效。
问题是我相信我正在覆盖数据集变量,并且只使用我检索的最后一个值填充数据网格。有没有更好的方法来实现我的目标?如果有,请解释。
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)
{
}
}
}
首先,可以使用数据集合并两个数据集。合并方法。这意味着您应该在循环之外创建一个新的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();
创建一个DataTable,并在for循环中添加返回的行
DataTable dt = new DataTable();
///…此处添加已知列
在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();
}
Try This
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();
实际上你的问题是你在每次迭代中绑定网格,否则代码没有问题。而不是:
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