Rania Umair
Rania Umair

Reputation: 2015

GridView Column not appearing

I am trying to add a new column in a GridView but it doesn't show. Here is the code-behind where I setup the DataTable and set it as the DataSource of the GridView:

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

Here is the GridView server-side markup code:

<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>

Upvotes: 0

Views: 3136

Answers (2)

Paul
Paul

Reputation: 1483

As this is WebForms you may need to execute your code before the form is painted in a method like Page_PreInit.

Upvotes: 0

El Ronnoco
El Ronnoco

Reputation: 11912

I would set AutoGenerateColumns = False and remove your entire <Columns>...</Columns> tag. As you are pretty much generating your own DataTable, just set that as the source and bind. ASP should do the rest.

Upvotes: 2

Related Questions