0
votes

I am going to find a value from a column in a gridview. If there are rows that have this value in that column, I will add those rows to a DataTable. Afterwards I will bind it to another gridview to display those rows out. However, when I tried to do this, columns that are in INT cannot be displayed out in INT but can only be displayed out in TEXT. Also, only 1 row is being binded to the datatable and displayed out in another gridview, I want many rows if many rows have the particular value in that column, not just 1 row.

This is the code I used:

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

Can someone please help me on this? Thanks a lot!!

1
GridView2.DataSource = dt; GridView2.DataBind(); place these lines outside of foreach loop blockAmit Mishra
Hi Amit, have tried to place those lines outside of foreach loop block, but no grid view appeared this time.Felicia Soh

1 Answers

2
votes

You should use GridView OnDataBound Event instead of RowDataBound. RowDataBound will be triggered after each row is bound to the Gridview where as OnDataBound will be called after all the rows are bound to the Gridview. Try the following

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