Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi, 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:
C#
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!!
Posted
Comments
Sinisa Hajnal 25-Sep-15 9:33am    
String is default type for columns without type. Use constructor new DataColumn(string columnname, type columntype)

1 solution

I think correct version would be

C#
new DataColumn("STATUS", GetType(int)); 


or something very similar (writing from memory.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900