Community discussion forum

Adding the Datatable values to GridView in .net 2005

  • 1 year ago

    currently i m using asp.net with c# (2.0 framework).
    here i created a datatable and put these values in gridview and i have displayed. But when i check the gridview row and column count, the row count is coming correctly. but the column count always remains 0. y? kindly help me... below i have attached the coding.........

    DataTable DTable;
    DataRow DRow;
    DataColumn DCol;

    sqlda=new System.Data.SqlClient.SqlDataAdapter("select tmp_values from tbl_table1", conn);
    sqlda.Fill(ds);

    DTable = new DataTable("Headers");

    DCol = new DataColumn("Head");
    DTable.Columns.Add(DCol);
    DRow = DTable.NewRow();
    for (int i = 0; i <ds.Tables[0].Rows.Count; i++)
    {
    DCol = new DataColumn("" + ds.Tables[0].Rows[i][0].ToString() + "");
    DTable.Columns.Add(DCol);
    DCol = new DataColumn("" + ds.Tables[0].Rows[i][0].ToString() + " " + " Remarks");
    DTable.Columns.Add(DCol);
    }

    DTable.Rows.Add(DRow);
    GridView1.DataSource = DTable;
    GridView1.DataBind();
    DTable.Columns.Count = 5 (its correct, but when bind with gridview and check the column count its zero)

    GridView1.Rows.Count = 1 (its correct)

    GridView1.Columns.Count = 0 (its incorrect, the answer should be 5)


    Help me!!! - KARAN
  • 1 year ago

    The following thread here suggests doing a Gridview1.HeaderRow.Cells.Count. Does that that help you at all?

    http://www.velocityreviews.com/forums/t295725-how-to-get-the-column-names-from-the-gridview.html


  • 1 year ago

    I think your approach to bind the gridview itself is wrong..

    As per your scenario:

                               DTable = new DataTable("Headers")

                               DTable.Columns[0] = ds.Tables[0].Columns[0];

                               DTable.Columns[1] = ds.Tables[0].Columns[1];

                               for (int i = 0; i<ds.tables[0].Rows.Count; i++)

                              {

                                     DRow = Dtable.NewRow();

                                     DRow[0] = ds.tables[0].rows[i][0].Tostring(); 

                                     DRow[1] = ds.tables[0].rows[i][1].Tostring();

                                    DTable.Rows.Add(DRow);

                            }

                          Gridivew1.Datasource = DTable;

                          Gridview1.Databind();

     

      

     

     

  • 1 year ago

    That's an interesting idea. Although if the databind approach was incorrect, I wouldn't have expected GridView1 to return a rowcount a 1 in the original code.


  • 1 year ago

    Yes Tim. i dont know how its arising and i dont know how the empty row is getting bind.......

  • 1 year ago

    Thanks TimL. but what the exact process i m doing is...

    i create a datatable which consist of dropdownlist box, textbox which will bind add to the datatable column at runtime and i merge the datatable to gridview. After adding to the gridview i cant fetch the dropdownlist value or Textbox value. How to proceed this...

    Help me plz. - KARAN

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback