【问题标题】:Bind a multi-dimensional ArrayList to a Gridview将多维 ArrayList 绑定到 Gridview
【发布时间】:2010-12-24 00:44:08
【问题描述】:

我有一个可用席位的 DataGrid,每个席位都有一个复选框,以便能够保留席位。在按钮单击事件中,如果单击 CheckBox,则将行的内容添加到 ArrayList,然后将 ArrayList 添加到会话,然后重定向到确认页面:

protected void Reserve_Click(object sender, EventArgs e)
{
    {
        ArrayList seatingArreaList = new ArrayList();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            Guid SeatId = (Guid)GridView1.DataKeys[i][0];
            CheckBox cbReserve = (CheckBox)GridView1.Rows[i].FindControl("cbReserve");
            Label lblSection = (Label)GridView1.Rows[i].FindControl("lblSection");
            Label lblRow = (Label)GridView1.Rows[i].FindControl("lblRow");
            Label lblPrice = (Label)GridView1.Rows[i].FindControl("lblPrice");

            if (cbReserve.Checked) 
            {
                string tempRowInfo = lblSection.Text + "|" + lblRow.Text + "|" + lblPrice.Text;
                seatingArreaList.Add(tempRowInfo);
            }
        }
        // Add the selected seats to a session
        Session["Seating"] = seatingArreaList;
    }
   Response.Redirect("Confirm.aspx?concertId=" + Request.QueryString["concertId"]);
}

在确认页面上,我想拆分这个数组并将其绑定到各自列中的另一个网格视图。

在确认页面上,存在一个会话,其中包含用管道分隔的三列,我正在努力将其拆分并将其绑定到确认网格。

请帮忙!

【问题讨论】:

    标签: asp.net data-binding gridview multidimensional-array arraylist


    【解决方案1】:

    这可能更容易创建一个DataTable,然后将其添加到会话变量中。重定向到确认页面后,只需将GridView 绑定到从会话变量中提取的DataTable

       protected void Reserve_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Section");
            dt.Columns.Add("Row");
            dt.Columns.Add("Price");
    
            {
                ArrayList seatingArreaList = new ArrayList();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    Guid SeatId = (Guid)GridView1.DataKeys[i][0];
                    CheckBox cbReserve = (CheckBox)GridView1.Rows[i].FindControl("cbReserve");
                    Label lblSection = (Label)GridView1.Rows[i].FindControl("lblSection");
                    Label lblRow = (Label)GridView1.Rows[i].FindControl("lblRow");
                    Label lblPrice = (Label)GridView1.Rows[i].FindControl("lblPrice");
    
                    if (cbReserve.Checked)
                    {
                        DataRow dr = dt.NewRow();
                        dr["Section"] = lblSection.Text;
                        dr["Row"] = lblRow.Text;
                        dr["Price"] = lblPrice.Text;
                        dt.Rows.Add(dr);
                    }
                }
                // Add the selected seats to a session
                Session["Seating"] = dt;
            }
            Response.Redirect("Confirm.aspx?concertId=" + Request.QueryString["concertId"]);
        }
    

    【讨论】:

    • 谢谢!使用该页面上的数据表是有意义的。我试图在确认页面上使用它,并将数组拆分为它。当然,现在我不需要数组了。谢谢:-)
    【解决方案2】:
     var q = from dto in seatingArreaList
         let z = dto.Split("|".ToCharArray())
         select z;
    

    然后把 q 放到网格上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-27
      • 2011-09-12
      • 1970-01-01
      • 2018-09-16
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      相关资源
      最近更新 更多