GridView列没有出现

时间:2011-11-02 10:03:39

标签: c# asp.net gridview

我正在尝试在GridView中添加一个新列,但它没有显示。这是我设置DataTable并将其设置为GridView的DataSource的代码隐藏:

SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
SqlCommand cmd1;
DateTime d = DateTime.Now;
cmd1 = new SqlCommand("Proc_PopulateTitle", connection);
cmd1.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd1;
da.Fill(dt);
dt.Columns.Add("Actions", Type.GetType("System.String"));
for (int i = 0; i < dt.Rows.Count; i++)
{                
    DateTime de = DateTime.Parse(dt.Rows[i][7].ToString());
    if (DateTime.Compare(de, d) < 0 || DateTime.Compare(de, d)==0)
    {
        dt.Rows[i]["Actions"] = "OPen";
    }
    else
    {
        dt.Rows[i]["Actions"] = "Expired";
    }
}
dt.GetChanges();
dt.AcceptChanges();


gvFaxView.DataSource = dt;

gvFaxView.DataBind();

这是GridView服务器端标记代码:

<asp:GridView ID="gvFaxView" runat="server" AllowPaging="True" AutoGenerateColumns="False"
    AutoGenerateSelectButton="True" CaptionAlign="Top" CssClass="text10" DataKeyNames="Fax_ID"
    PageSize="5" OnPageIndexChanging="gvFaxView_PageIndexChanging" OnSelectedIndexChanged="gvFaxView_SelectedIndexChanged">
    <FooterStyle BackColor="#348EC2" Font-Bold="True" ForeColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Fax Title">
            <ItemTemplate>
                <asp:Label ID="lbgvName" runat="server" Text='<%# Eval("Fax_Title") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Fax Date">
            <ItemTemplate>
                <asp:Label ID="lbgvType" runat="server" Text='<%# Eval("Fax_Date") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Fax Status">
            <ItemTemplate>
                <asp:Label ID="lbgvfName" runat="server" Text='<%# Eval("Fax_status") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Fax Expire Date">
            <ItemTemplate>
                <asp:Label ID="lbgvNic" runat="server" Text='<%# Eval("Fax_Exp_Date") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Open/Expire">
            <ItemTemplate>
                <asp:Label ID="lbgvopenexp" runat="server" Text="Open"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <EditRowStyle BackColor="#999999" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <PagerStyle BackColor="Maroon" ForeColor="White" HorizontalAlign="Center" />
    <HeaderStyle BackColor="Maroon" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

2 个答案:

答案 0 :(得分:2)

我会设置AutoGenerateColumns = False并删除整个<Columns>...</Columns>标记。由于您几乎生成了自己的DataTable,因此只需将其设置为源和绑定即可。 ASP应该做其余的事情。

答案 1 :(得分:0)

由于这是WebForms,您可能需要在使用Page_PreInit等方法绘制表单之前执行代码。