c# - Getting name of table columns in gridview in addition to custom names -
i have simple grid on aspx page , binding data coming select query on button click event. not sure how bind columns of table grid getting 8 columns, 4 header given in aspx page , 4 headers of table columns. below button click event code.
protected void btnsearch_click(object sender, eventargs e) { mybooklistcont mybooklistcont = new mybooklistcont(); gdvmybooks.datasource = mybooklistcont.searchbookdetailscont(); gdvmybooks.databind(); } and below aspx code of gridview.
<asp:gridview id="gdvmybooks" runat="server"> <columns> <asp:boundfield datafield="bk_nm" headertext="book name" /> <asp:boundfield datafield="athr_nm" headertext="author name" /> <asp:boundfield datafield="buy_yr" headertext="buy year" /> <asp:boundfield datafield="price" headertext="price" /> </columns> </asp:gridview> looks silly question, appreciated.
this because have explicitly specified 4 columns in design code :
<columns> <asp:boundfield datafield="bk_nm" headertext="book name" /> <asp:boundfield datafield="athr_nm" headertext="author name" /> <asp:boundfield datafield="buy_yr" headertext="buy year" /> <asp:boundfield datafield="price" headertext="price" /> </columns> now here, can see, there headertext property override table column name , display text mentioned in here.
you have couple of options here can try :
first, if want columns custom header text, can define other columns these 4 in same manner well. display data , column headers per given format.something :
<asp:gridview id="gdvmybooks" autogeneratecolumns="false" runat="server"> <columns> <asp:boundfield datafield="bk_nm" headertext="book name" /> <asp:boundfield datafield="athr_nm" headertext="author name" /> <asp:boundfield datafield="buy_yr" headertext="buy year" /> <asp:boundfield datafield="price" headertext="price" /> //other columns using same syntax above. </columns> </asp:gridview> also, if dont want bind columns coming in query, can use attibute in gridview,
autogeneratecolumns = false and specify columns need have done.
the other thing is, if want straight away bind result in query gridview, remove 4 boundfield statements , leave is. bind table gridview same header names column names.
i hope makes things clear.
Comments
Post a Comment