将行从GridView绑定到DataTable并在另一个GridView RSS中显示它

时间:2015-09-25 09:47:44

标签: c# asp.net gridview data-binding datatable

我将从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();
                    }

有人可以帮我这个吗?非常感谢!!

1 个答案:

答案 0 :(得分:2)

您应该使用GridView OnDataBound事件而不是RowDataBound。在每行绑定到Gridview之后将触发RowDataBound,因为在将所有行绑定到Gridview之后将调用OnDataBound。请尝试以下

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