user229775
user229775

Reputation: 21

Binding a DataTable to Template Fields of a GridView

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();

Upvotes: 2

Views: 20801

Answers (2)

Md Shahnawaz
Md Shahnawaz

Reputation: 3

     foreach (GridViewRow grdRow in grdMenuPermitted.Rows)
        {
            DataRow drow = dt.NewRow();

            Label lblMenuName = (Label)grdRow.FindControl("lblMenuName");

            HiddenField hdnID = (HiddenField)grdRow.FindControl("hdnID");

            drow["MenuName"] = lblMenuName.Text;//grdRow.Cells[0].Text;                
            drow["MenuID"] = hdnID.Value;
            //drow["lname"] = grdRow.Cells[3].Text;
            dt.Rows.Add(drow);
        }

Upvotes: -1

Pragnesh Khalas
Pragnesh Khalas

Reputation: 2898

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>

Upvotes: 4

Related Questions