none
C# Gridview Summary total within RowDataBound RRS feed

  • Question

  • am trying to display the summary total at the footer of my grid view. I am using the GridView_RowDataBound that's auto generated when you click into it from the properties menu.

    I am able to view the summary total of the gridview in the data when I manually input what the header will be and this works but I want to make it dynamic so it will work with different header.

    The code below works and will display the total in the footer but only for "JimsAccount"

    int total = 0;
    protected void GridViewUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    
        if (e.Row.RowType == DataControlRowType.DataRow && DataBinder.Eval(e.Row.DataItem, "JimsAccount") != System.DBNull.Value)
        {
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "JimsAccount"));
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            System.Diagnostics.Debug.WriteLine("In footer");
            e.Row.Cells[0].Text = "Grand Total";
            e.Row.Cells[0].Font.Bold = true;
    
            e.Row.Cells[1].Text = total.ToString();
            e.Row.Cells[1].Font.Bold = true;
        }
    
    }

    To sum up, works when I manually input the headers along with their positions but I can't get it to work dynamically.

    • Moved by CoolDadTx Monday, January 28, 2019 4:58 PM ASP.NET related
    Saturday, January 26, 2019 4:57 PM

All replies

  • Has anyone been able to do create a footer in a Grid view with the summary?

    I found this article on it: https://docs.microsoft.com/en-us/dotnet/api/system.data.datarow?view=netframework-4.7.2

    But I haven't been able to follow it effectively.

    • Merged by KareninstructorMVP Sunday, January 27, 2019 10:57 AM Same question asked twice
    Sunday, January 27, 2019 10:33 AM
  • Hello,

    If you mean DataGridView in regards to footer for a Grid then there are three choices, write your own, use a free user built custom DataGridView found here or purchase a third party Grid such as found here.

    Best option to start with is option two.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, January 27, 2019 10:54 AM
  • Hello,

    First off double posting is frowned upon in these forums and secondly in your second post you were unclear to what type of grid.

    For ASP.NET look at the following and if that does not assist use the ASP.NET developer forums.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, January 27, 2019 11:04 AM
  • Hi

    private void Gcount()
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                   
                    DataGridViewRowHeaderCell cell = dataGridView1.Rows[i].HeaderCell;
                   
                    cell.Value = (i + 1).ToString();
                    //dataGridView1.Columns[0].HeaderText = "ر.م";
                    dataGridView1.Rows[i].HeaderCell.Style.ForeColor = Color.Blue;
                    dataGridView1.Rows[i].HeaderCell.Style.BackColor = Color.Yellow;
                    dataGridView1.Rows[i].HeaderCell = cell;
                }
            }

    Please click the Mark as answer button and vote as helpful if this reply solves your problem

    Best Regards.

    Sunday, January 27, 2019 11:08 AM
  • Hi LITTIE KILLER,

    Thank you for posting here.

    According your post, your question is more relevant to ASP.NET, please post in the following forum.

    https://forums.asp.net/

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 28, 2019 9:25 AM