c# - Change Header Text in Column of Gridview pulling from Database -
hi wondering how can change header text of column in gridview when pulling database build gridview.
here how building gridview.
sqlconnection conn = new sqlconnection("removed"); sqldatareader rdr = null; string commandstring = "select ordernumber, createddate, createdby, customerid, store_number, package dbo.orderheader"; try { conn.open(); sqlcommand cmd = new sqlcommand(commandstring, conn); rdr = cmd.executereader(); gridview1.datasource = rdr; gridview1.databind(); } catch (exception ex) { // log error } { if (rdr != null) { rdr.close(); } if (conn != null) { conn.close(); } } }
get header row object
in databound
event , change desired name,
void gridview1_databound(object sender, eventargs e) { // header row. gridviewrow headerrow = gridview1.headerrow; headerrow.cells[0].text = "order"; headerrow.cells[1].text = "date"; }
or
set autogeneratecolumns
false
, use column bound fields
,
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false"> <columns> <asp:boundfield headertext="order" datafield="ordernumber" /> <asp:boundfield headertext="date" datafield="createddate" /> </columns> </asp:gridview>
Comments
Post a Comment