Click here to Skip to main content
15,887,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to bind my grid column header names by retrieving data from a table. This table has two fields,ID and ExpenseName, I want to display the ExpenseNames as Column header of the Grid. Actually I am creating Staff grid view. I want all the Expense names of staff to be displayed as a header.

Please Give me some ideas.

Thanks in advance.
Posted
Updated 16-Jan-13 20:01pm
v2

C#
DataTable dt = GetTable();
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Columns[0].HeaderText = dt.Columns[0].ColumnName;
 
Share this answer
 
Hi,

Please bind your grid as below.

C#
DataSet ds = GetExpenseName();
if(ds!=null&&ds.Table[0].Rows.count>0)
{
  for(int i=0;i<ds.table[0].rows.count;i++)>
  {
    GridViewName.Column[i].HeaderText = ds.Table[0].Row[i]["ExpenseNames"].ToString();
  }
}

Hope this will help you.
 
Share this answer
 
C#
protected void gvSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
  {

    try
     {
        if (e.Row.RowType == DataControlRowType.Header)
          {
              Label lblSearchResults =            (Label)e.Row.FindControl("lblSearchResults");
              lblSearchResults.Text = ((DataRowView)e.Row.DataItem)["Column Name"].ToString();
          }
     }
}



This might work for you
 
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