Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

GridView Total Field

Hello,

Is there a way to include a Total field below all of the data from the BoundFields? Example below.

Column 1             Column 2
User1                  $5
User2                  $10

Total: $15
Avatar of guru_sami
guru_sami
Flag of United States of America image

Avatar of Brian

ASKER

Hi guru_sami,

Anyway you could help me determine where I need to implement the footer template? I do know that It needs to be the footer for the gvOrders GridView.


        <asp:Panel CssClass="grid" ID="pnlCust" runat="server">
                                <asp:UpdatePanel ID="pnlUpdate" runat="server">
                                    <ContentTemplate>
                                        <asp:GridView Width="100%" ID="gvCustomers" AutoGenerateColumns="False"
                                            runat="server" OnRowDataBound="gvCustomers_RowDataBound"
                                        CellPadding="4" EnableModelValidation="True" ForeColor="#5A5A5A"
                                            GridLines="None"
                                            EmptyDataText="The Student may not have been enrolled for the 2008-2009 LI Courses.">
                                            <AlternatingRowStyle BackColor="White" HorizontalAlign="Left" Height="20px" />
                                            <Columns>
                                                <asp:TemplateField HeaderText="Course Name">
                                                    <ItemTemplate>
                                                        <asp:Panel CssClass="group" ID="pnlCustomer" runat="server">
                                                            <asp:Image ID="imgCollapsible" CssClass="first" ImageUrl="~/plus_sign.png" runat="server" />
                                                            <span class="header"><%#Eval("COURSE_NAME")%></span>
                                                        </asp:Panel>
                                                        <div id="menu">
                                                        <asp:Panel ID="pnlOrders" runat="server" Style="margin-left: 20px; margin-right: 20px;" HorizontalAlign="Left">
                                                            <asp:GridView AutoGenerateColumns="False" CssClass="grid" ID="gvOrders"
                                                                runat="server" GridLines="None"
                                                            EnableViewState="False" CellPadding="4" HorizontalAlign="Left" ShowFooter="true"
                                                                EnableModelValidation="True" ForeColor="#333333" Width="400px">
                                                                <FooterStyle BackColor="#FFFFFF" Font-Bold="True" ForeColor="White" />
                                                                <HeaderStyle Font-Bold="True" ForeColor="White"
                                                                    HorizontalAlign="Left" />
                                                                <PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Center" />
                                                                <RowStyle CssClass="row" Height="20px" BackColor="#F0F0F0"
                                                                    ForeColor="#5A5A5A" />
                                                                <AlternatingRowStyle CssClass="altrow" HorizontalAlign="Left" Height="20px"
                                                                    BackColor="White"/>
                                                                <Columns>
                                                                    <asp:TemplateField>
                                                                    <%--<ItemTemplate>
                                                                            <%# Container.DataItemIndex + 1 %>
                                                                        </ItemTemplate>
                                                                        <ItemStyle CssClass="rownum" />--%>
                                                                    </asp:TemplateField>                                                              
                                                                    <asp:BoundField HeaderText="Assignment" DataField="ASSIGNTITLE">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="213px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                    <asp:BoundField HeaderText="Pts. Possible" DataField="PNTSPOSSIBLE">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="93px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                    <asp:BoundField HeaderText="Pts. Earned" DataField="PNTSEARNED">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="93px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                </Columns>
                                                                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                                                            </asp:GridView>

                                                        </asp:Panel>
                                                        </div>
                                                        <asp:CollapsiblePanelExtender ID="cpe" runat="Server" TargetControlID="pnlOrders" CollapsedSize="0" Collapsed="True" ExpandControlID="pnlCustomer"
                                                        CollapseControlID="pnlCustomer" AutoCollapse="False" AutoExpand="False" ScrollContents="false" ImageControlID="imgCollapsible"
                                                            ExpandedImage="~/minus_sign.png" CollapsedImage="~/plus_sign.png" ExpandDirection="Vertical">
                                                        </asp:CollapsiblePanelExtender>
                                                    </ItemTemplate>
                                                    <HeaderStyle HorizontalAlign="Left" CssClass="pacybergradesheader"
                                                        Height="20px" />
                                                </asp:TemplateField>
                                                <asp:BoundField DataField="FINALGRADE" HeaderText="Final Grade"
                                                    FooterText="Total Points">
                                                <HeaderStyle CssClass="pacybergradesheadernopad" Height="20px"
                                                    HorizontalAlign="Left" />
                                                </asp:BoundField>
                                            </Columns>
                                            <FooterStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" Height="20px" HorizontalAlign="Left" />
                                            <HeaderStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" Height="20px" HorizontalAlign="Left" />
                                            <PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Left" Height="20px" />
                                            <RowStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" Height="20px" HorizontalAlign="Left" />
                                            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" Height="20px" HorizontalAlign="Left" />
                                        </asp:GridView>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                            </asp:Panel>
following the link i provide earlier you need to add this to your<Columns> of gvOrders:
<FooterTemplate>
  <%# GetTotal().ToString("N2") %>
</FooterTemplate>

Here is one more sample: http://msdn.microsoft.com/en-us/library/ms972833.aspx
Avatar of Brian

ASKER

Ok, I applied the footer in the gvOrders GridView like you should and like the tutorial displayed but it's not showing up and I even added ShowFooter="true" on that GridView. Please see where I added the Footer.

 <asp:Panel CssClass="grid" ID="pnlCust" runat="server">
                                <asp:UpdatePanel ID="pnlUpdate" runat="server">
                                    <ContentTemplate>
                                        <asp:GridView Width="100%" ID="gvCustomers" AutoGenerateColumns="False"
                                            runat="server" OnRowDataBound="gvCustomers_RowDataBound"
                                        CellPadding="4" EnableModelValidation="True" ForeColor="#5A5A5A"
                                            GridLines="None"
                                            EmptyDataText="The Student may not have been enrolled for the 2008-2009 LI Courses.">
                                            <AlternatingRowStyle BackColor="White" HorizontalAlign="Left" Height="20px" />
                                            <Columns>
                                                <asp:TemplateField HeaderText="Course Name">
                                                    <ItemTemplate>
                                                        <asp:Panel CssClass="group" ID="pnlCustomer" runat="server">
                                                            <asp:Image ID="imgCollapsible" CssClass="first" ImageUrl="~/plus_sign.png" runat="server" />
                                                            <span class="header"><%#Eval("COURSE_NAME")%></span>
                                                        </asp:Panel>
                                                        <div id="menu">
                                                        <asp:Panel ID="pnlOrders" runat="server" Style="margin-left: 20px; margin-right: 20px;" HorizontalAlign="Left">
                                                            <asp:GridView AutoGenerateColumns="False" CssClass="grid" ID="gvOrders"
                                                                runat="server" GridLines="None"
                                                            EnableViewState="False" CellPadding="4" HorizontalAlign="Left" ShowFooter="true"
                                                                EnableModelValidation="True" ForeColor="#333333" Width="400px">
                                                                <FooterStyle BackColor="#FFFFFF" Font-Bold="True" ForeColor="White" />
                                                                <HeaderStyle Font-Bold="True" ForeColor="White"
                                                                    HorizontalAlign="Left" />
                                                                <PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Center" />
                                                                <RowStyle CssClass="row" Height="20px" BackColor="#F0F0F0"
                                                                    ForeColor="#5A5A5A" />
                                                                <AlternatingRowStyle CssClass="altrow" HorizontalAlign="Left" Height="20px"
                                                                    BackColor="White"/>
                                                                <Columns>
                                                                    <asp:TemplateField>
                                                                    <%--<ItemTemplate>
                                                                            <%# Container.DataItemIndex + 1 %>
                                                                        </ItemTemplate>
                                                                        <ItemStyle CssClass="rownum" />--%>
                                                                        <FooterTemplate>
<%#Eval("finalpntspossible")%>
</FooterTemplate>

                                                                    </asp:TemplateField>                                                              
                                                                    <asp:BoundField HeaderText="Assignment" DataField="ASSIGNTITLE">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="213px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                    <asp:BoundField HeaderText="Pts. Possible" DataField="PNTSPOSSIBLE">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="93px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                    <asp:BoundField HeaderText="Pts. Earned" DataField="PNTSEARNED">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="93px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                </Columns>
                                                                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                                                            </asp:GridView>

                                                        </asp:Panel>
                                                        </div>
                                                        <asp:CollapsiblePanelExtender ID="cpe" runat="Server" TargetControlID="pnlOrders" CollapsedSize="0" Collapsed="True" ExpandControlID="pnlCustomer"
                                                        CollapseControlID="pnlCustomer" AutoCollapse="False" AutoExpand="False" ScrollContents="false" ImageControlID="imgCollapsible"
                                                            ExpandedImage="~/minus_sign.png" CollapsedImage="~/plus_sign.png" ExpandDirection="Vertical">
                                                        </asp:CollapsiblePanelExtender>
                                                    </ItemTemplate>
                                                    <HeaderStyle HorizontalAlign="Left" CssClass="pacybergradesheader"
                                                        Height="20px" />
                                                </asp:TemplateField>
                                                <asp:BoundField DataField="FINALGRADE" HeaderText="Final Grade"
                                                    FooterText="Total Points">
                                                <HeaderStyle CssClass="pacybergradesheadernopad" Height="20px"
                                                    HorizontalAlign="Left" />
                                                </asp:BoundField>
                                            </Columns>
                                            <FooterStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" Height="20px" HorizontalAlign="Left" />
                                            <HeaderStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" Height="20px" HorizontalAlign="Left" />
                                            <PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Left" Height="20px" />
                                            <RowStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" Height="20px" HorizontalAlign="Left" />
                                            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" Height="20px" HorizontalAlign="Left" />
                                        </asp:GridView>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                            </asp:Panel>
Avatar of Brian

ASKER

Hi guru_sami,

Below is an example of what I have so far, however I need to only display the value from the DB and not calcuate on the code, only display FINALGRADEPOSSIBLE from DB in Total field. The GridView below is NOT my GridView it is an example that I found. The field that I need to display from my DB is called FINALGRADEPOSSIBLE.

<script runat="server">
    int FINALPNTSPOSSIBLE;
    int GetUnitPrice(int Price)
    {
        FINALPNTSPOSSIBLE += Price;
        return Price;
    }
    int GetTotal()
    {
        return FINALPNTSPOSSIBLE;
    }
</script>

<asp:GridView ID="GridView1" ShowFooter="true" DataKeyNames="ProductId" AutoGenerateColumns="false"
            runat="server" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Productid" HeaderText="Product Id" />
                <asp:BoundField DataField="ProductName" FooterText="Total" HeaderText="Product Name" />
                <asp:TemplateField HeaderText="Unit Price" FooterStyle-Font-Bold="True">
                    <ItemTemplate>
                        <%# GetUnitPrice(decimal.Parse(Eval("UnitPrice").ToString())).ToString("N2")  %>
                    </ItemTemplate>
                    <FooterTemplate>
                        <%# GetTotal().ToString("N2") %>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
Then adjust your GetTotal function to return value from database...

    int GetTotal()
    {
         //Query your database to get final points and return it.
        return FINALPNTSPOSSIBLE;
    }
Avatar of Brian

ASKER

Ok, this is what I'm using below to retrieve the data. Not sure how to implement that code to retrieve the value. I'll try and see if I can figure it out unless you have time to let me know.

protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            string COURSE_ID;
            COURSE_ID = (string)drv["COURSE_ID"];

            string STUDENT_ID;
            STUDENT_ID = (string)drv["STUDENT_ID"];

            string connString = ConfigurationManager.ConnectionStrings["PACyber"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            string sqlCommandText = "BB_SELECT_ASSIGNMENTS";
            SqlCommand cmd = new SqlCommand(sqlCommandText, conn);
            cmd.CommandType = CommandType.StoredProcedure;

            //retrieve values for course_id & student_id
            cmd.Parameters.AddWithValue("@COURSE_ID", COURSE_ID);
            cmd.Parameters.AddWithValue("@STUDENT_ID", STUDENT_ID);

            try
            {
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
                gvOrders.DataSource = reader;
                gvOrders.DataBind();
            }

            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

            finally
            {
                conn.Close();
            }
        }
    }
from where does the FINALPNTSPOSSIBLE come from?
One other way is as shown in the second link I provided. Inside the RowDataBound check if it is Footer then set the value.

 else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[0].Text = "Totals:";
         e.Row.Cells[1].Text = FINALPNTSPOSSIBLE;
    }
Avatar of Brian

ASKER

>> from where does the FINALPNTSPOSSIBLE come from?
It comes from my DB and the FINALPNTSPOSSIBLE  value is include in the BB_SELECT_ASSIGNMENTS Stored Procedure withing the gvCustomers_RowDataBound Event Handler code.

Where should I include the code below at within my code?

else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[0].Text = "Totals:";
         e.Row.Cells[1].Text = FINALPNTSPOSSIBLE;
    }
not sure I am understanding it right ...but I think it should be going inside gvOrders rowdatabound
so you need to add Onrowdatabound event handler as you have for gvCustomers.
Avatar of Brian

ASKER

I'm probally not explainging it to you correctly, let me try again.

Do I need to place the code snippet that you supplied within the protected void gvCustomers_RowDataBound code? If so, where at within the gvCustomers_RowDataBound code?
No not in gvCustomers_RowDataBound but you have to add similar new handler for your second GV - gvOrders. Then place that code snipped inside that because from your code I understand that the FINALPNTSPOSSIBLE is related to gvOrders and not the main gvCustomers.
Avatar of Brian

ASKER

Ok, If you take a look at the my GridView below that I just made changes to I'm able to retrieve the FINALGRADEPOSSIBLE value without writing any codee, HOWEVER, I can't get that value into the Footer at the bottom. It keeps going into the first column with the other value.


<asp:GridView Width="100%" ID="gvCustomers" AutoGenerateColumns="False"
                                            runat="server" OnRowDataBound="gvCustomers_RowDataBound"
                                        CellPadding="4" EnableModelValidation="True" ForeColor="#5A5A5A"
                                            GridLines="None"
                                            EmptyDataText="The Student may not have been enrolled for the 2008-2009 LI Courses.">
                                            <AlternatingRowStyle BackColor="White" HorizontalAlign="Left" Height="20px" />
                                            <Columns>
                                                <asp:TemplateField HeaderText="Course Name">
                                                    <ItemTemplate>
                                                        <asp:Panel CssClass="group" ID="pnlCustomer" runat="server">
                                                            <asp:Image ID="imgCollapsible" CssClass="first" ImageUrl="~/plus_sign.png" runat="server" />
                                                            <span class="header"><%#Eval("COURSE_NAME")%></span>
                                                        </asp:Panel>                                                    
                                                        <asp:Panel ID="pnlOrders" runat="server" Style="margin-left: 20px; margin-right: 20px;" HorizontalAlign="Left">                                          
                                                            <asp:GridView AutoGenerateColumns="False" CssClass="grid" ID="gvOrders"
                                                                runat="server" GridLines="None"
                                                            EnableViewState="False" CellPadding="4" HorizontalAlign="Left" ShowFooter="True"
                                                                EnableModelValidation="True" ForeColor="#333333" Width="400px">
                                                                <FooterStyle BackColor="#FFFFFF" Font-Bold="True" ForeColor="White" />
                                                                <HeaderStyle Font-Bold="True" ForeColor="White"
                                                                    HorizontalAlign="Left" />
                                                                <PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Center" />
                                                                <RowStyle CssClass="row" Height="20px" BackColor="#F0F0F0"
                                                                    ForeColor="#5A5A5A" />
                                                                <AlternatingRowStyle CssClass="altrow" HorizontalAlign="Left" Height="20px"
                                                                    BackColor="White"/>
                                                                <Columns>
                                                                    <asp:TemplateField>
                                                                    <ItemTemplate>
                                                                     <%-- <%# Container.DataItemIndex + 1 %>  --%>  
                                                                                                                                       
                                                                    </ItemTemplate>
                                                                    <ItemStyle CssClass="rownum" />
                                                                        <FooterTemplate>
                                                                       
                                                                          <%#Eval("finalpntspossible")%>

                                                                        </FooterTemplate>                                                                      
                                                                    </asp:TemplateField>                                                                                                                          
                                                                    <asp:BoundField HeaderText="Assignment" DataField="ASSIGNTITLE">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="213px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                    <asp:BoundField HeaderText="Pts. Possible" DataField="PNTSPOSSIBLE">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="93px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                    <asp:BoundField HeaderText="Pts. Earned" DataField="PNTSEARNED">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="93px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                </Columns>
                                                                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                                                            </asp:GridView>  
                                                            </div>                                                    
                                                        </asp:Panel>                                              
                                                        <asp:CollapsiblePanelExtender ID="cpe" runat="Server" TargetControlID="pnlOrders" CollapsedSize="0" Collapsed="True" ExpandControlID="pnlCustomer"
                                                        CollapseControlID="pnlCustomer" AutoCollapse="False" AutoExpand="False" ScrollContents="false" ImageControlID="imgCollapsible"
                                                            ExpandedImage="~/minus_sign.png" CollapsedImage="~/plus_sign.png" ExpandDirection="Vertical">
                                                        </asp:CollapsiblePanelExtender>
                                                    </ItemTemplate>
                                                    <HeaderStyle HorizontalAlign="Left" CssClass="pacybergradesheader"
                                                        Height="20px" />
                                                </asp:TemplateField>
                                                <asp:BoundField DataField="FINALGRADE" HeaderText="Final Grade"
                                                    FooterText="Total Points">
                                                <HeaderStyle CssClass="pacybergradesheadernopad" Height="20px"
                                                    HorizontalAlign="Left" />
                                                </asp:BoundField>
                                            </Columns>
                                            <FooterStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" Height="20px" HorizontalAlign="Left" />
                                            <HeaderStyle BackColor="#026FA7" Font-Bold="True" ForeColor="White" Height="20px" HorizontalAlign="Left" />
                                            <PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Left" Height="20px" />
                                            <RowStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" Height="20px" HorizontalAlign="Left" />
                                            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" Height="20px" HorizontalAlign="Left" />
                                        </asp:GridView>
can you share a screenshot of what you are seeing with your above code.....
Avatar of Brian

ASKER

Ok, I have attached a screenshot showing that the Total Points value is 1000 and it's displaying beside the Assignment Title value and I have a BoundField Footer Text = "Final Grade" and would like the 1000 value to display next to the footer instead of displaying as a column.
data.gif
ok just for test try this to see what I told earlier...

1: add  OnRowDataBound="gvOrders_RowDataBound" to your gvOrders:

<asp:GridView AutoGenerateColumns="False" CssClass="grid" ID="gvOrders"
 runat="server" GridLines="None" EnableViewState="False" CellPadding="4" HorizontalAlign="Left" ShowFooter="True" EnableModelValidation="True" ForeColor="#333333" Width="400px"
OnRowDataBound="gvOrders_RowDataBound">

2: Add this to your code-behind:

protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
           e.Row.Cells[1].Text = "1000";
        }        
}

Note: Though I am not clear exactly where you want that 1000 to appear corresponding to your figure.
Avatar of Brian

ASKER

Ok,

1.) I Added OnRowDataBound and it worked.
2.) The code behind worked as well


>> Note: Though I am not clear exactly where you want that 1000 to appear corresponding to your figure.

I need to display the value 1000 next to the Name of the Footer labeled "Final Grade". Right now the 1000 value is located at the very bottom of the first column of the GridView.

Also, I will need to add another value other than the one we are working with now. Now, how can i remove the actually hard coded value of 1000 and replace that with the actually value from the DB?
Try to modify the code-behind like below:

int fpPossible= 0;
protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            fpPossible= (Decimal)drv["finalpntspossible"];
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[2].Text = fpPossible.ToString();
        }
}

2: Adjust the gvOrders databinding code you provided in your comment#28570257 like below:

try
 {
            conn.Open();
            DataTable dt = new DataTable();
            SqlDataReader reader = cmd.ExecuteReader();
            dt.Load(reader);
            GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
            gvOrders.DataSource = dt;
            gvOrders.DataBind();
  }

3: Remove this code from your Markup:

<asp:TemplateField>
<ItemTemplate>
 <%-- <%# Container.DataItemIndex + 1 %>  --%>  
</ItemTemplate>
<ItemStyle CssClass="rownum" />
<FooterTemplate>
 <%#Eval("finalpntspossible")%>
 </FooterTemplate>                                                                      
</asp:TemplateField>  

-->I need to display the value 1000 next to the Name of the Footer labeled "Final Grade". Right now the 1000 value is located at the very bottom of the first column of the GridView.

Do you mean "Final Points" ?
you can adjust the cell Index in the code statement:  e.Row.Cells[2].Text = fpPossible.ToString();

Avatar of Brian

ASKER

Ok, that worked out great....

How can I add another field below Final Grades with the value beside it?
Avatar of Brian

ASKER

Ok, I was able to modify the code below to include the other value that I need but it displays the values side be side rather than up and down. Also, it does not display the Footer names beside the values.

    protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            fpPossible = (int)drv["FINALPNTSPOSSIBLE"];
            fpEarned = (decimal)drv["FINALPNTSEARNED"];
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[1].Text = fpPossible.ToString();
            e.Row.Cells[2].Text = fpEarned.ToString();
        }
    }
I am not understanding where and exactly how you want the footer to appear.

You can add FooterTemplate to the desired column e.g.

 <FooterTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Label"></Label>                                                                      
</FooterTemplate>

Then in code-behind you can set those label text:

if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lbl1 = (Label)e.Row.Cells[6].FindControl("Label1");
            lbl1.Text = "Points Possible";
            Label lbl2 = (Label)e.Row.Cells[6].FindControl("Label2");
            lbl2.Text = "Points Earned";
            e.Row.Cells[1].Text = fpPossible.ToString();
            e.Row.Cells[2].Text = fpEarned.ToString();
        }

Note: The above code is just a blind-shot because I really don't understand how you want to display.
Avatar of Brian

ASKER

Ok, The Label controls are above each other which is what I needed, it would be nice if they would follow under the second column but if that's just a formating issue then I can probally figure it out. The only other problem is that I receive the following error when running the code.

ERROR CODE:

Specified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: indexSpecified argument was out of the range of valid values. Parameter name: index
Avatar of Brian

ASKER

Ok, I made the following corrections and the code will return and return both values and labels but they do not line up with each other. I'm going to attach another screenshot.

NEW CODE:

    protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            fpPossible = (int)drv["FINALPNTSPOSSIBLE"];
            fpEarned = (decimal)drv["FINALPNTSEARNED"];
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lbl1 = (Label)e.Row.Cells[1].FindControl("Label1");
            lbl1.Text = "Points Possible";
            Label lbl2 = (Label)e.Row.Cells[2].FindControl("Label2");
            lbl2.Text = "Points Earned";
            e.Row.Cells[1].Text = fpPossible.ToString();
            e.Row.Cells[2].Text = fpEarned.ToString();
        }
    }


data1.gif
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

Yes!!! That is exactly what I needed. Is there anyway that I can format them such as making the values look like this below? It appears that both Label controls and the returned values are in one column, it would be nice to somehow stack the labels in one column and then display the values in the next column.

Points Possible: 1000
Points Earned: 686.00

Right now them look like the following below.

Points
Possible:
1000
Points
Earned:
686.00
you might want to set the width of the column or may be move your FooterTemplate into second column i.e. the Assignment Title column instead.

e.g.:

<asp:TemplateField>                                                <ItemTemplate>
    <%#Eval("ASSIGNTITLE")%> 
</ItemTemplate>
<FooterTemplate>                                                  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>                                                      </FooterTemplate>
</asp:TemplateField>

Open in new window

Avatar of Brian

ASKER

Not sure what you mean by 'move your FooterTemplate into second column'.

I have a width column for the assignment field but that doesn't seem to make the difference.
did you try my last code?
can you share the latest  ..just gvOrders markup?
Avatar of Brian

ASKER

I wasn't sure where I had to add that at. Below is teh gvOrder Markup.


<asp:GridView AutoGenerateColumns="False" CssClass="grid" ID="gvOrders"
                                                                runat="server" GridLines="None"
                                                            EnableViewState="False" CellPadding="4" HorizontalAlign="Left" ShowFooter="True"
                                                                EnableModelValidation="True" ForeColor="#333333" Width="500px" OnRowDataBound="gvOrders_RowDataBound">
                                                                <FooterStyle BackColor="#FFFFFF" Font-Bold="True" ForeColor="White" />
                                                                <HeaderStyle Font-Bold="True" ForeColor="White"
                                                                    HorizontalAlign="Left" />
                                                                <PagerStyle BackColor="#F0F0F0" ForeColor="#5A5A5A" HorizontalAlign="Center" />
                                                                <RowStyle CssClass="row" Height="20px" BackColor="#F0F0F0"
                                                                    ForeColor="#5A5A5A" />
                                                                <AlternatingRowStyle CssClass="altrow" HorizontalAlign="Left" Height="20px"
                                                                    BackColor="White"/>
                                                                <Columns>
                                                                    <asp:TemplateField>
                                                                    <ItemTemplate>
                                                                    <%--  <%# container.dataitemindex + 1 %>  --%>                                                                                                                      
                                                                    </ItemTemplate>
                                                                    <ItemStyle cssclass="rownum" />
                                                                    <FooterTemplate>    
                                                                      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
                                                                      <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>                                                                    
                                                                    </FooterTemplate>                                                                      
                                                                    </asp:TemplateField>                                                                                                                          
                                                                    <asp:BoundField HeaderText="Assignment" DataField="ASSIGNTITLE">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="300px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                    <asp:BoundField HeaderText="Pts. Possible" DataField="PNTSPOSSIBLE">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="80px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                    <asp:BoundField HeaderText="Pts. Earned" DataField="PNTSEARNED">
                                                                        <HeaderStyle HorizontalAlign="Left" Width="80px"
                                                                        CssClass="gvOrdersHeader" />
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                </Columns>
                                                                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                                                            </asp:GridView>  
1:Removed First templated Column.
2: Changed "Assignment" BoundField to TemplateField and moved FooterTemplate inside it.
Check the attached file:
New-Text-Document.txt
Avatar of Brian

ASKER

Thank you VERY MUCH for all your help!!! I can't thank you enough for helping me all the way through. Keep up the GREAT work that you do!!!!