Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Sum columns in datagrid's footer

Sum columns in datagrid's footer

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    viktor9990
    wrote on last edited by
    #1

    In an autogenerated datagrid I will be showing an unknow number of colums and rows with info as statistics as well the total of the rows in the Datagrid's footer. I know how to do that if the autogenerated colums is false. How to show these totals in the footer if the Datagrids autogenerated colums is true? Thanks Here is my code: Void ComputeSum(Object sender, DataGridItemEventArgs e) { 'First, make sure we are dealing with an Item or AlternatingItem if (e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem) { 'Snip out the ViewCount int viewCount = _ Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ViewCount")) viewCountSum += viewCount } }

    M 1 Reply Last reply
    0
    • V viktor9990

      In an autogenerated datagrid I will be showing an unknow number of colums and rows with info as statistics as well the total of the rows in the Datagrid's footer. I know how to do that if the autogenerated colums is false. How to show these totals in the footer if the Datagrids autogenerated colums is true? Thanks Here is my code: Void ComputeSum(Object sender, DataGridItemEventArgs e) { 'First, make sure we are dealing with an Item or AlternatingItem if (e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem) { 'Snip out the ViewCount int viewCount = _ Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ViewCount")) viewCountSum += viewCount } }

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, To display the summary in the footer of the datagrid, you can add a control such as Label to the footer of the generated column to display the info. Also, you should remember to set the ShowFooter property to true since its default value is false. The sample code is something like:

      private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
      {
      if(e.Item.ItemType == ListItemType.Footer)
      {
      //Your code here to add a Label control to the footer at a specified cell.
      ...
      }
      }

      private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
      {
      if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
      //Your code here to count the summary value.
      ...
      }else if(e.Item.ItemType == ListItemType.Footer)
      {
      //Your code here to get the Label control in the footer and have it display the summary info.
      ...
      }
      }

      V 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, To display the summary in the footer of the datagrid, you can add a control such as Label to the footer of the generated column to display the info. Also, you should remember to set the ShowFooter property to true since its default value is false. The sample code is something like:

        private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
        {
        if(e.Item.ItemType == ListItemType.Footer)
        {
        //Your code here to add a Label control to the footer at a specified cell.
        ...
        }
        }

        private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
        //Your code here to count the summary value.
        ...
        }else if(e.Item.ItemType == ListItemType.Footer)
        {
        //Your code here to get the Label control in the footer and have it display the summary info.
        ...
        }
        }

        V Offline
        V Offline
        viktor9990
        wrote on last edited by
        #3

        The problem is that I have an autogenerated columns datagrid so the number of columns displayed in the grid will vary from time to time. It will be difficult to Show each column's total in the footer because the number of columns is unknown. How can I determine in which footer column I will show which total of the column? Thanks

        M 1 Reply Last reply
        0
        • V viktor9990

          The problem is that I have an autogenerated columns datagrid so the number of columns displayed in the grid will vary from time to time. It will be difficult to Show each column's total in the footer because the number of columns is unknown. How can I determine in which footer column I will show which total of the column? Thanks

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          You have no idea about the number of the columns, but you do know the column name to sum up the values, so you basically can determine the index value of the column in the datagrid. To specify the column index, you'll have two options here IMO: + You can determine the index based on the datasource, for example you bind a DataTable to the grid, each column in the data table is displayed by a column in the grid in the exact order. So you can easily specify the index value of the column. + You can determine the index based on the datagrid control, because you set the AutoGenerateColumn to true, so at runtime a databound column is automatically generated to be bound to a data field in the data source. The name of the data field is displayed as text in the header of the bound column, you can specify the index value of the column based on this info. The sample code looks something like:

          private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
          {
          if(e.Item.ItemType == ListItemType.Footer)
          {
          //You can add Label controls for all cells since you don't know the column index.
          for(int index=0; index<e.Item.Cells.Count; index++)
          {
          Label lbl = new Label();
          e.Item.Cells[index].Controls.Add(lbl);
          }
          }
          }

          private ArrayList columnNames;
          private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
          {
          if(e.Item.ItemType == ListItemType.Header)
          {
          //You need to save all the data field names.
          columnNames = new ArrayList();
          for(int index=0; index<e.Item.Cells.Count; index++)
          {
          string colName = e.Item.Cells[index].Text;
          columnNames.Add(colName);
          }
          }else if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
          {
          //You code here to sum up the specified values.
          ...
          }else if(e.Item.ItemType == ListItemType.Footer)
          {
          //Determine the column index and display the summary info.
          int colIndex = columnNames.IndexOf("ViewCount");
          Label lbl = e.Item.Cells[colIndex].Controls[0] as Label;
          lbl.Text = viewCountSum.ToString();
          }
          }

          V 1 Reply Last reply
          0
          • M minhpc_bk

            You have no idea about the number of the columns, but you do know the column name to sum up the values, so you basically can determine the index value of the column in the datagrid. To specify the column index, you'll have two options here IMO: + You can determine the index based on the datasource, for example you bind a DataTable to the grid, each column in the data table is displayed by a column in the grid in the exact order. So you can easily specify the index value of the column. + You can determine the index based on the datagrid control, because you set the AutoGenerateColumn to true, so at runtime a databound column is automatically generated to be bound to a data field in the data source. The name of the data field is displayed as text in the header of the bound column, you can specify the index value of the column based on this info. The sample code looks something like:

            private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
            if(e.Item.ItemType == ListItemType.Footer)
            {
            //You can add Label controls for all cells since you don't know the column index.
            for(int index=0; index<e.Item.Cells.Count; index++)
            {
            Label lbl = new Label();
            e.Item.Cells[index].Controls.Add(lbl);
            }
            }
            }

            private ArrayList columnNames;
            private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
            if(e.Item.ItemType == ListItemType.Header)
            {
            //You need to save all the data field names.
            columnNames = new ArrayList();
            for(int index=0; index<e.Item.Cells.Count; index++)
            {
            string colName = e.Item.Cells[index].Text;
            columnNames.Add(colName);
            }
            }else if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
            //You code here to sum up the specified values.
            ...
            }else if(e.Item.ItemType == ListItemType.Footer)
            {
            //Determine the column index and display the summary info.
            int colIndex = columnNames.IndexOf("ViewCount");
            Label lbl = e.Item.Cells[colIndex].Controls[0] as Label;
            lbl.Text = viewCountSum.ToString();
            }
            }

            V Offline
            V Offline
            viktor9990
            wrote on last edited by
            #5

            I have used now the following to sum every autogenerated column in the datagrid but the totals in the footer columns are not right!Any help is appreciated. protected void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.Footer) { //You can add Label controls for all cells since you don't know the column index. for(int index=0; index= columnNames.Count ==false) { viewCountSum += viewCount; } i += 1; } else if(e.Item.ItemType == ListItemType.Footer) { for(int i= 0; i< columnNames.Count; i++) { //Determine the column index and display the summary info. int colIndex = columnNames.IndexOf(columnNames[i].ToString()); Label lbl = e.Item.Cells[colIndex].Controls[0] as Label; lbl.Text = viewCountSum.ToString(); } } } //Here is the data source public DataSet CreateDataSource() { DataSet ds = new DataSet(); DataTable dt = new DataTable(); DataColumn dc; DataRow dr; dt.TableName="People"; //One way to create a datacolumn dc = new DataColumn(); dc.ColumnName="PersonID"; dc.DataType=System.Type.GetType("System.Int32"); dt.Columns.Add(dc); //Another way to create a datacolumn via its //overloaded constructor dc = new DataColumn("PersonAge",System.Type.GetType("System.Int32")); dt.Columns.Add(dc); dc = new DataColumn("PersonNameExtra",System.Type.GetType("System.Int32")); dt.Columns.Add(dc); //Now set the primary key for the table using PersonID DataColumn[] dtPK = new DataColumn[1]; dtPK[0] = dt.Columns["PersonID"]; dt.Primary

            M 1 Reply Last reply
            0
            • V viktor9990

              I have used now the following to sum every autogenerated column in the datagrid but the totals in the footer columns are not right!Any help is appreciated. protected void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.Footer) { //You can add Label controls for all cells since you don't know the column index. for(int index=0; index= columnNames.Count ==false) { viewCountSum += viewCount; } i += 1; } else if(e.Item.ItemType == ListItemType.Footer) { for(int i= 0; i< columnNames.Count; i++) { //Determine the column index and display the summary info. int colIndex = columnNames.IndexOf(columnNames[i].ToString()); Label lbl = e.Item.Cells[colIndex].Controls[0] as Label; lbl.Text = viewCountSum.ToString(); } } } //Here is the data source public DataSet CreateDataSource() { DataSet ds = new DataSet(); DataTable dt = new DataTable(); DataColumn dc; DataRow dr; dt.TableName="People"; //One way to create a datacolumn dc = new DataColumn(); dc.ColumnName="PersonID"; dc.DataType=System.Type.GetType("System.Int32"); dt.Columns.Add(dc); //Another way to create a datacolumn via its //overloaded constructor dc = new DataColumn("PersonAge",System.Type.GetType("System.Int32")); dt.Columns.Add(dc); dc = new DataColumn("PersonNameExtra",System.Type.GetType("System.Int32")); dt.Columns.Add(dc); //Now set the primary key for the table using PersonID DataColumn[] dtPK = new DataColumn[1]; dtPK[0] = dt.Columns["PersonID"]; dt.Primary

              M Offline
              M Offline
              minhpc_bk
              wrote on last edited by
              #6

              + The data binding behaviour of the datagrid control runs in the way that each data row of the data source is bound to a single data grid item, each value of the data row is bound to a cell of the data grid item. + Take a look at again your sample code below: else if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { int i = 0; int viewCount = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, columnNames[i].ToString())); if(i >= columnNames.Count ==false) { viewCountSum += viewCount; } i += 1; } The scope of the variable i is inside the if statement, when data is bound to the next row, the execution gets in the if statement again, and at this time the variable is recreated with the 0 value. So you are likely to sum up all the values of the first column and have a single interger variable viewCountSum hold the value. If you want to sum the values for each column seperately, you might consider using an arraylist object to hold the sum values instead of a single variable. private ArrayList sumValues; protected void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemType == ListItemType.Header) { //You need to save all the data field names. columnNames = new ArrayList(); for(int index=0; index

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups