首页 > 解决方案 > 如何在gridview中检查不同行中的数据

问题描述

我目前正在从数据库中获取数据,然后如果我选中了超过 2 行的复选框,它将汇总 SENDQTY 的总数并将详细信息从 GridView3 发送到 GridView4。现在如何检查 2 行中的 STOCKCODE 是否不同,它会提示错误消息?

protected void showGridView()
{
    int match = 0;
    int total = 0;
          
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[9] { new DataColumn("DODetailID"), new DataColumn("SALESORDERNO"), new DataColumn("STOCKCODE"), new DataColumn("SENDQTY"), new DataColumn("scanflag"), new DataColumn("REJECT"), new DataColumn("DispatchOrderID"), new DataColumn("DispatchDATE"), new DataColumn("DELORDERNO") });
    foreach (GridViewRow row in GridView3.Rows)
    {
         if (row.RowType == DataControlRowType.DataRow)
         {
              CheckBox CheckBox1 = (row.Cells[0].FindControl("CheckBox1") as CheckBox);

              //int qty = Convert.ToInt32(row.Cells[4].Text);
              if (CheckBox1.Checked)
              {
                  string DODetailID = (row.Cells[1].FindControl("DODetailID") as Label).Text;
                  string SALESORDERNO = (row.Cells[2].FindControl("SALESORDERNO") as Label).Text;
                  string STOCKCODE = (row.Cells[3].FindControl("STOCKCODE") as Label).Text;                 
                  int SENDQTY = Convert.ToInt32((row.Cells[4].FindControl("SENDQTY") as Label).Text);
                  CheckBox scanflag = (row.Cells[5].FindControl("scanflag") as CheckBox);
                  string REJECT = (row.Cells[6].FindControl("REJECT") as Label).Text;
                  string DispatchOrderID = (row.Cells[7].FindControl("DispatchOrderID") as Label).Text;
                  string DispatchDATE = (row.Cells[8].FindControl("DispatchDATE") as Label).Text;
                  string DELORDERNO = (row.Cells[9].FindControl("DELORDERNO") as Label).Text;
                  Session["STOCKCODE"] = STOCKCODE.ToString();
                       
                  dt.Rows.Add(DODetailID, SALESORDERNO, STOCKCODE, SENDQTY, scanflag, REJECT, DispatchOrderID, DispatchDATE, DELORDERNO);
                  match++;
                       
                  total = total + SENDQTY;
                  GridView4.DataSource = dt;
                  GridView4.DataBind();  
             }                   
         }
     }       
 }

protected void btnContinue_Click(object sender, EventArgs e)
{  
     showGridView();
     mp3.Show(); // this is showing GridView4
}

标签: c#asp.net

解决方案


您需要为每个循环运行两个。第一次对您计算复选框(和股票代码)的数据运行 for/each 循环。然后,您可以在代码中决定运行/使用现有循环。

Collection MyCheckList = New Collection;
Boolean ListBad = false;
foreach (GridViewRow row in GridView3.Rows)
{
     if (row.RowType == DataControlRowType.DataRow)
     {
          CheckBox CheckBox1 = (row.Cells[0].FindControl("CheckBox1") as CheckBox);
          Label txtStock = (row.Cells[0].FindControl("STOCKCODE") as Label);
         if CheckBox.Checked {
            if MyCheckList.Contains(txtStock.Text) {
               ListBad = true;
               Exit for;
            else
               ListBad.Add(txtSTock)
            }

等等等等等等

等等等等

换句话说,对数据运行测试/循环 - 如果选中,将 stockcode 放入集合中,如果这样的 stockcode 已经存在,那么您知道存在超过 1 个已检查给定股票,因此您的标志 ListBad = true当您完成该循环过程时。

您还可以将收集想法添加到现有循环中。退出时,您知道是否检查了一个以上的股票代码。因此,您可能不需要单独的第一个循环,但如果您检查已经存在的股票代码,则必须“建立”一个已检查 + 股票代码的列表。因此,在该集合中建立一个检查列表,因此您知道是否使用了股票代码 + 在该列表中多次检查。


推荐阅读