关于c#:DataTable中的文本未显示在GridView中

Text in DataTable not displayed in GridView

我正在创建一个DataTable并填充其中的行/列。

但是,当我将其绑定到GridView时,看不到分配给一列的文本。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        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();

gridview仅显示第一列(编号为1-3),而第二列的文本不会显示在gridview上。

有什么想法吗?

谢谢!

编辑:第一列gridview在BoundField中,而第二列在TemplateFied ... ItemTemplate中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   <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>


    <ItemTemplate>
        </asp:TextBox>
    </ItemTemplate>
    <FooterStyle HorizontalAlign="Right" />
    <FooterTemplate>
     
    </FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>


您尚未使用gridview中的第二列。
请使用另一个绑定字段,如下所示:

1
 

或按以下说明修改您的模板

1
2
3
4
 <ItemTemplate>
    '  runat="server" Width="300">    
    </asp:TextBox>
</ItemTemplate>

编辑1

的结构很奇怪,因为

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

内部。


您可以像这样标记。根据您的要求使用标签/文本框

1
2
3
<ItemTemplate>
 </asp:Label>
</ItemTemplate> </asp:TemplateField>