c# - Binding a DataTable to Template Fields of a GridView -
i can bind datatable gridview auto generating columns, need display multi-lined cell 1 of columns. this, want use template field item template using textbox object. fill in datatable adding columns , adding rows. know datatable set right because shows data (except multi-lined cell) want it. issue getting gridview extract data based on column names , filling in template fields have set up. if turn autogeneratecolumns off 4 templatefield columns still appear (in correct amount according datatable too), blank, , if have set true, 4 blank columns appear 4 additional columns same headers contain data using defaults cell contains display information.
<asp:gridview id="datagrid1" runat="server" autogeneratecolumns="false" headerstyle-borderstyle="none" cellpadding="3" itemstyle-wrap="true" > <columns> <asp:templatefield headertext="code"> <asp:itemtemplate> <asp:label id="txtcode" runat="server" text='<%# eval("code") %>'> </asp:label> </asp:itemtemplate> </asp:templatefield> <asp:templatefield headertext="title"> <asp:itemtemplate> <asp:label id="txttitle" runat="server" text='<%# eval("title") %>'> </asp:label> </asp:itemtemplate> </asp:templatefield> <asp:templatefield headertext="class"> <asp:itemtemplate> <asp:label id="txtclass" runat="server" text='<%# eval("class") %>'> </asp:label> </asp:itemtemplate> </asp:templatefield> <asp:templatefield headertext="history"> <asp:itemtemplate> <asp:textbox id="txthistory" runat="server" isreadonly="true" text='<%# eval("history")%>'> </asp:textbox> </asp:itemtemplate> </asp:templatefield> </columns> </asp:gridview>
the above portion of asp.net code relates gridview in question. following how set datatable , bind it.
datatable table = new datatable(); table.columns.add("code", typeof(string)); table.columns.add("title", typeof(string)); table.columns.add("class", typeof(string)); table.columns.add("history", typeof(string)); (int = 0; < index; i++) { table.rows.add(docs[i].code, docs[i].name, docs[i].class.name, history[i]); } datagrid1.datasource = table; datagrid1.databind();
change .aspx code below
<columns> <asp:templatefield headertext="code"> <itemtemplate> <asp:label id="txtcode" runat="server" text='<%# eval("code") %>'> </asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="title"> <itemtemplate> <asp:label id="txttitle" runat="server" text='<%# eval("title") %>'> </asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="class"> <itemtemplate> <asp:label id="txtclass" runat="server" text='<%# eval("class") %>'> </asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="history"> <itemtemplate> <asp:textbox id="txthistory" runat="server" isreadonly="true" text='<%# eval("history")%>'> </asp:textbox> </itemtemplate> </asp:templatefield> </columns>
Comments
Post a Comment