C# – Binding a DataTable to Template Fields of a GridView

asp.netcdatatablegridview

I can successfully bind a DataTable to a GridView by auto generating columns, but I need to display a multi-lined cell for one of the columns. To do this, I want to use a template field with an item template using a TextBox object. I fill in the DataTable by adding columns and then adding the rows. I know my datatable is set up right because it shows all the data (except for the multi-lined cell) as I want it. My issue is getting the gridview to extract my data based on the column names and filling in the template fields I have set up. If I turn AutoGenerateColumns off then the 4 templatefield columns still appear (in the correct amount according to the datatable too), just blank, and if I have it set to true, then the 4 blank columns appear as well as 4 additional columns with the same headers that contain my data using the defaults for what the cell contains to display the 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 is the portion of my asp.net code that relates to the GridView in question. Following is how i set up my DataTable and 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));
for (int i = 0; i < index; i++)
{
    table.Rows.Add(docs[i].Code, docs[i].Name, docs[i].Class.Name, history[i]);
}
DataGrid1.DataSource = table;
DataGrid1.DataBind();

Best Answer

Change .aspx code like 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>
Related Topic