绑定行从GridView到DataTable和显示它在另一个GridView RSS

本文关键字:GridView 另一个 RSS 显示 DataTable 绑定 | 更新日期: 2023-09-27 18:14:34

我要从gridview中的列中找到一个值。如果在该列中有具有此值的行,我将把这些行添加到DataTable中。然后我将它绑定到另一个gridview以显示这些行。然而,当我尝试这样做时,在INT中显示的列不能在INT中显示,而只能在TEXT中显示。此外,只有1行被绑定到数据表并在另一个gridview中显示出来,我想要许多行,如果许多行在该列中具有特定值,而不仅仅是1行。

这是我使用的代码:

    public void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
 DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add(new DataColumn("DATE"));
            dt.Columns.Add(new DataColumn("CODE"));
            dt.Columns.Add(new DataColumn("PERSON_NAME"));
            dt.Columns.Add(new DataColumn("STATUS"));
            dt.Columns.Add(new DataColumn("HOBBIES"));
            dt.Columns.Add(new DataColumn("SCORE"));
            dt.Columns.Add(new DataColumn("ITEM"));
            dt.Columns.Add(new DataColumn("QUANTITY"));
            dt.Columns.Add(new DataColumn("TYPE"));
            dt.Columns.Add(new DataColumn("RATING"));
            dt.Columns.Add(new DataColumn("PRICE"));
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "STATUS")) == "Regular")
                    {
                        dr = dt.NewRow();
                        dr["DATE"] = e.Row.Cells[0].Text;
                        dr["CODE"] = e.Row.Cells[1].Text;
                        dr["PERSON_NAME"] = e.Row.Cells[2].Text;
                        dr["STATUS"] = e.Row.Cells[3].Text;
                        dr["HOBBIES"] = e.Row.Cells[4].Text;
                        dr["SCORE"] = e.Row.Cells[5].Text;
                        dr["ITEM"] = e.Row.Cells[6].Text;
                        dr["QUANTITY"] = e.Row.Cells[7].Text;
                        dr["TYPE"] = e.Row.Cells[8].Text;
                        dr["RATING"] = e.Row.Cells[9].Text;
                        dr["PRICE"] = e.Row.Cells[10].Text;
                        dt.Rows.Add(dr);
                        GridView2.DataSource = dt;
                        GridView2.DataBind();
                    }
有人能帮我一下吗?非常感谢!!

绑定行从GridView到DataTable和显示它在另一个GridView RSS

你应该使用GridView ondataboundevent而不是rowdataboundevent。RowDataBound将在每一行绑定到Gridview后触发,而OnDataBound将在所有行绑定到Gridview后调用。试试下面的

public void GridView1_OnDataBound(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add(new DataColumn("DATE"));
    dt.Columns.Add(new DataColumn("CODE"));
    dt.Columns.Add(new DataColumn("PERSON_NAME"));
    dt.Columns.Add(new DataColumn("STATUS"));
    dt.Columns.Add(new DataColumn("HOBBIES"));
    dt.Columns.Add(new DataColumn("SCORE"));
    dt.Columns.Add(new DataColumn("ITEM"));
    dt.Columns.Add(new DataColumn("QUANTITY"));
    dt.Columns.Add(new DataColumn("TYPE"));
    dt.Columns.Add(new DataColumn("RATING"));
    dt.Columns.Add(new DataColumn("PRICE"));
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        if (gvr.Cells[3].Text == "Regular")
        {
            dr = dt.NewRow();
            dr["DATE"] = gvr.Cells[0].Text;
            dr["CODE"] = gvr.Cells[1].Text;
            dr["PERSON_NAME"] = gvr.Cells[2].Text;
            dr["STATUS"] = gvr.Cells[3].Text;
            dr["HOBBIES"] = gvr.Cells[4].Text;
            dr["SCORE"] = gvr.Cells[5].Text;
            dr["ITEM"] = gvr.Cells[6].Text;
            dr["QUANTITY"] = gvr.Cells[7].Text;
            dr["TYPE"] = gvr.Cells[8].Text;
            dr["RATING"] = gvr.Cells[9].Text;
            dr["PRICE"] = gvr.Cells[10].Text;
            dt.Rows.Add(dr);
        }
    }
    GridView2.DataSource = dt;
    GridView2.DataBind();
}