1
votes

I am creating a DataTable and populating the rows/columns in it.

However when I bind it to a GridView, I cannot see the text I assign to one column.

Code:

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));

        DataRow dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = "some text";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["RowNumber"] = 2;
        dr["Column1"] = "more text";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["RowNumber"] = 3;
        dr["Column1"] = "and more text";
        dt.Rows.Add(dr);

        Gridview2.DataSource = dt;
        Gridview2.DataBind();

The gridview only displays the first Column (with the numbers 1-3) but the 2nd column with the text does not display on the gridview.

Any ideas?

Thanks!

EDIT: The 1st column gridview is in BoundField whereas the 2nd column is within TemplateFied...ItemTemplate.

   <asp:gridview ID="Gridview2" runat="server" ShowFooter="true" GridLines="None"  
            AutoGenerateColumns="false"  >
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <EditRowStyle BackColor="#999999" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="#" ItemStyle-Width="30px" ItemStyle-HorizontalAlign="Center" />
<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Width="300"></asp:TextBox>
    </ItemTemplate>
    <FooterStyle HorizontalAlign="Right" />
    <FooterTemplate>
     <asp:Button ID="ButtonAdd1" runat="server" Text="Add Statement" onclick="ButtonAdd1_Click" />
    </FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
2
can you please post the aspx markup code of gridview.Nag

2 Answers

1
votes

You have not used the second column in your gridview. Either use another bound field as follows

<asp:BoundField DataField="Column1" HeaderText="#" ItemStyle-Width="30px" />

Or Modify your template filed as follows

 <ItemTemplate>
    <asp:TextBox ID="TextBox1" Text='<%# Eval("Column1") %>'  runat="server" Width="300">     
    </asp:TextBox>
</ItemTemplate>

Edit 1

Your structure of the gridview is strange as you have

 <FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
 <asp:Button ID="ButtonAdd1" runat="server" Text="Add Statement" 
             onclick="ButtonAdd1_Click" />
</FooterTemplate>

inside <asp:TemplateField>.

0
votes

You can do like this the Markup. Use label/textbox based on your requirement

<asp:TemplateField HeaderText="Column1">  
<ItemTemplate> 
 <asp:Label ID="lblColumn1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Column1")%'></asp:Label>
</ItemTemplate> </asp:TemplateField>