GridView Header Text in asp.net

只谈情不闲聊 提交于 2020-01-01 10:14:50

问题


Hey guys, i want to change the header text of the gridview using Design.. from <TemplateField HeaderText="">

i created a variable in code behind which is public and set the value in that variable and then i tried to call that variable over here as below:

<TemplateField HeaderText = '<%= VariableCallHere %>'

but while running page i got <%= VariableCallHere %> as a header text even i tried changing using gridView1.HeaderRow.Cells[0].Text = "text Here" (This Throws object reference error)

Any one have any suggestions how this could be achieved..


回答1:


It should be gridview1.Columns[ColumnIndex].HeaderText = "Header text";




回答2:


For this, in the RowDataBound event of the gridview control you need to write as like follows:

if (e.Row.RowType == DataControlRowType.Header)
{
  e.Row.Cells[0].Text = "column 1";
  e.Row.Cells[1].Text = "column 2";
  .....
}



回答3:


I'm using it like this for multilingual it works fine NO need of extra work for iterating the rows just put it there and let it do the work

<asp:BoundField DataField="TITLE_NAME" HeaderText="<%$ Resources:Site,lblTitleName %>"
                            ItemStyle-Width="20%">
   <HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>



回答4:


Access it via the columns collection:

gridview1.Columns[Index].HeaderText= "text Here";

As in:

gridview1.Columns[0].HeaderText= "text Here";



回答5:


       if (e.Row.RowType == DataControlRowType.Header)
        {                
            Label lblAddInText = (Label)e.Row.FindControl("lblAddInText");
            lblAddInText.Text = "ADD IN TEXT" 
        }

in RowCreated Event of Gridview. Using and Label in .



来源:https://stackoverflow.com/questions/5476148/gridview-header-text-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!