locked
Solving this problem RRS feed

  • Question

  • User-1425882342 posted

     

       I created an access database (stored.mdb) which has the following table:

       Table Name: Stock

       Table fields:

                      ItemNo :    Integer                  P.K

                      ItemDesc. :  String

                      Quantity : Integer

                     ItemPrice : Single

                  

     

       I add these following data to the table Stok as

      ItemNo        ItemDesc               Quantity              ItemPrice              

     1001           Spark Plug                2000                    9.95                        

     1002           Shock Absorber         120                      319.00                   

     1003           HeadLight                  150                      45.90                    

     1009           Breake Pad               89                        89.00                 

     

    we need to Create a web Page to display the following results as it is

     

       ItemNo.                ItemDescription                             Subtotal
    -------------------------------------------------------------------------------------------------
        1001                    SparkPlug                                    19900.00            $

       

    ---------------------------------------------------------------------------------------------------
    Total                                                                           --------------            $

     

    I used this sql statment when i use gridview wizard to create the previous table

    sql="select ItemNo ,   ItemDesc , (Quantity*ItemPrice) as subtotal from Stock "  

     

    but my question how can I apper the total of subtotals in the gridview without write any code

     

    Thank you

    Sunday, October 26, 2008 7:44 AM

Answers

  • User-1472696755 posted

    this is the correct vb.net code:

    Protected Sub GridView1_DataBound(sender As Object, e As EventArgs)
        GridView1.FooterRow.Cells(2).Text = "0"
        For Each row As GridViewRow In GridView1.Rows
            GridView1.FooterRow.Cells(2).Text = (Integer.Parse(GridView1.FooterRow.Cells(2).Text) + Integer.Parse(row.Cells(2).Text)).ToString()
        Next
       
    End Sub

    C# code:

     protected void GridView1_DataBound(object sender, EventArgs e)
            {
                GridView1.FooterRow.Cells[2].Text = "0";
                foreach (GridViewRow row in GridView1.Rows)
                {
                    GridView1.FooterRow.Cells[2].Text = (int.Parse(GridView1.FooterRow.Cells[2].Text) + int.Parse(row.Cells[2].Text)).ToString();
                }
               
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 27, 2008 2:26 AM

All replies

  • User1564875471 posted

     You need to place a label control in the Gridview Footer template , and then in the GridView DataBound event handler , you need to conenct to the database using System.Data.SqlClient classes and get the Subtotal amount so that you can dispaly it in the label.

    You can use the SqlDataReader to get the subtotal from the database 

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

    Sunday, October 26, 2008 8:11 AM
  • User-1425882342 posted

    Hi aggen

     

    thanks  Mr.Anas for your quick replay 

     

    I alredy did the way you tell me . I place a label and i name it total and at the properity of grid view i add this code

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

     

    Try

    total.Text = Integer.Parse(total.Text) + Integer.Parse(e.Row.Cells(2).Text)

    Catch

    End Try

    End Sub

     and that code work excellent and I can sum the total of subtotals in database and the result was saved in the label correctly (Note: the result was out of gridview)

    my problem now  that i can't place the label at the footer of gridview to be the total exactly at the bottom of subtotal field , when I try to edit the gridview template and insert the label to pager template of gridview nothing happend i don't know why ?

     

    I need the result which I get to be in gridview

    Sunday, October 26, 2008 11:06 AM
  • User-1472696755 posted

    you can do the following to solve your problem:

    first thing delete the Total label

    after that add the following code to the page_load sub:

     

    GridView1.FooterRow.Cells(2).Text = "0"

     

    then replace the line:

    total.Text = Integer.Parse(total.Text) + Integer.Parse(e.Row.Cells(2).Text) 

     with this line:

    GridView1.FooterRow.Cells(2).Text = Integer.Parse(GridView1.FooterRow.Cells(2).Text) + Integer.Parse(e.Row.Cells(2).Text) 
     by doing that the footer of the gridview will show the total.
    Sunday, October 26, 2008 11:54 AM
  • User-1425882342 posted

    Dear Mr.Khaled

     

    I did your way and it was ok.  now the total was in the footer of the gridview but with no summation of subtotals and i get this error

     Object reference not set to an instance of an object.A <?XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" /><MSHelp:link tabIndex=0 keywords="T:System.NullReferenceException">NullReferenceException</MSHelp:link>  was unhandled by user code

    this error happend when i put your code

     

    GridView1.FooterRow.Cells(2).Text = Integer.Parse(GridView1.FooterRow.Cells(2).Text) + Integer.Parse(e.Row.Cells(2).Text)

     

    ThankYou

    Sunday, October 26, 2008 12:16 PM
  • User-1472696755 posted

    Sorry for the mistak the FooterRow is not created yet in this RowDataBound Event

    Please remove the old code and then

    Select the DataBound Event in the datagrid and add this code to the event:

     

    GridView1.FooterRow.Cells[2].Text = "0"
    For Each (Dim row as GridViewRow in GridView1.Rows)
            GridView1.FooterRow.Cells(2).Text = integer.Parse(GridView1.FooterRow.Cells(2).Text) + integer.Parse(row.Cells(2).Text)
    Next
                
    
     
    Sunday, October 26, 2008 12:55 PM
  • User-1425882342 posted

    Dear Kahled

     I removed the old code and I put this code in databound event for gridview  as the following

    Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound

    GridView1.FooterRow.Cells[2].Text = "0"

    For Each (Dim row as GridViewRow in GridView1.Rows)

    GridView1.FooterRow.Cells(2).Text = Integer.Parse(GridView1.FooterRow.Cells(2).Text) + Integer.Parse(row.Cells(2).Text)

    Next

    End Sub

     

    when I compile it the following error come 

    Error 1 Property access must assign to the property or use its value
    Error 2 Identifier expected.
    Error 3 Expression expected.  

     

    thank y

    Sunday, October 26, 2008 4:48 PM
  • User-1472696755 posted

    this is the correct vb.net code:

    Protected Sub GridView1_DataBound(sender As Object, e As EventArgs)
        GridView1.FooterRow.Cells(2).Text = "0"
        For Each row As GridViewRow In GridView1.Rows
            GridView1.FooterRow.Cells(2).Text = (Integer.Parse(GridView1.FooterRow.Cells(2).Text) + Integer.Parse(row.Cells(2).Text)).ToString()
        Next
       
    End Sub

    C# code:

     protected void GridView1_DataBound(object sender, EventArgs e)
            {
                GridView1.FooterRow.Cells[2].Text = "0";
                foreach (GridViewRow row in GridView1.Rows)
                {
                    GridView1.FooterRow.Cells[2].Text = (int.Parse(GridView1.FooterRow.Cells[2].Text) + int.Parse(row.Cells[2].Text)).ToString();
                }
               
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 27, 2008 2:26 AM
  • User-1425882342 posted

    Thanks Mr.Khaled

     

    Your code was ok . it works good that what I need

     

    [:)]

    Friday, October 31, 2008 10:24 AM