Click here to Skip to main content
14,980,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helo friends,
How to create header of gridview dynamically at runtime?
i have written some code on my .cs file but i dont know what to write in my .aspx file..please helpme.
Thanks in advance.
Posted

HI,

Here is a link which explains the dynamic grid creation with the headers please check this.

Dynamic gridview with headers

Thanks
   
In .cs file where you use sql query for gridview datasource like
SQL
select id, name,email from tablename 


By default query column name is your gridview header if you want to change it then you can use query as

SQL
select id as ID , name as [Customer Name] , email as Email from tablename



Please dont forget to Mark it as ans if you get help from it
Thanks
Hemant Singh
   
v2
Comments
rajinder raviya 3-Apr-13 14:47pm
   
yes its worked!
thanks!
Your question is confusing. You say you have written some code in .cs file but do not know what to write in .aspx file? The title suggest you want to create gridview's header dynamically. For that bit try the following code:
C#
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView grd = (GridView)sender;

GridViewRow HeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Insert);

TableCell HeaderCell = new TableCell();
HeaderCell.Text = "First Column";
HeaderCell.Style["font-weight"] = "bold";
HeaderRow.Cells.Add(HeaderCell);

HeaderCell = new TableCell();
HeaderCell.Text = "Second Column";
HeaderCell.Style["font-weight"] = "bold";
HeaderRow.Cells.Add(HeaderCell);

HeaderCell = new TableCell();
HeaderCell.Text = "Third Column";
HeaderCell.Style["font-weight"] = "bold";
HeaderRow.Cells.Add(HeaderCell);

grd.Controls[0].Controls.AddAt(0, HeaderRow);
}
}


For any other requirement than this, please edit your question and post again.
   
Comments
Anuragintit 13-May-19 6:58am
   
i m having drop down in my grid view, and when inserting the data with submit button, my loop start and getting drop down value as null which is selected by user, and when loop reaches to second time i get the drop down value , kindly help to sort out

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