• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2045
  • Last Modified:

Access RowDataBound when Gridview is with Repeater

Hi

I have a Gridview with a Repeater and am trying to total up some values in the gridview. In the past i have used something like below.

However How can I do this when the Gridview is with the repeater. I have use the repeater "ItemDataBound" event to add new columns to the gridview programmically but am not sure how to total the columns.

Any help and code will be useful

Thanks


-- USED BEFORE
 
	Protected Sub gvFIMStats_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvFIMStats.RowDataBound
		If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "NumberFunds")) Then
			intNoFundsTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "NumberFunds"))
       
		End If
		If e.Row.RowType = DataControlRowType.Footer Then
			e.Row.Cells(0).Text = "Total:"
			e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Right
			e.Row.Cells(1).Text = intNoFundsTotal.ToString	'("d")               
			e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
			e.Row.Font.Bold = True
 
		End If
 
End Sub

Open in new window

0
patelajk
Asked:
patelajk
  • 8
  • 8
1 Solution
 
Bob LearnedCommented:
You might be able to loop through the GridView rows, and sum a running total from the values in the column that you are working with.
0
 
patelajkAuthor Commented:
any ideas on how i can do.... any sample code would be very helpful
0
 
Bob LearnedCommented:
Sample:

Dim grid As GridView = TryCast(sender, GridView)
Dim sum As Decimal = 0
For Each row As GridViewRow In grid.Rows
     Dim drv As DataRowView = TryCast(row.DataItem, DataRowView)
     Dim value As Decimal = Val(drv("Price"))
     sum += value
Next row
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
patelajkAuthor Commented:
sorry going to need some more help on my repearter databound event i have the code below.

How do i implement your sample into this:

	Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
		Dim Grid As GridView
		Grid = e.Item.FindControl("gvClientInfo")
		
		If Grid IsNot Nothing Then
 
			If Grid.Rows.Count > 0 Then
				'add the top column headings to this table
				Dim dgitem As New GridViewRow(1, 0, ListItemType.Header, DataControlRowState.Normal)
				Dim tblCell As New TableHeaderCell 'TableCell
 
				tblCell.Text = ""
				tblCell.ColumnSpan = 1
				tblCell.HorizontalAlign = HorizontalAlign.Left
				tblCell.CssClass = "testw"
				dgitem.Cells.Add(tblCell)
				
				tblCell = New TableHeaderCell
				tblCell.Text = "Amount Committed"
				tblCell.ColumnSpan = 2
				tblCell.HorizontalAlign = HorizontalAlign.Center
				tblCell.CssClass = "testw"
				dgitem.Cells.Add(tblCell)
 
end if
 
end if

Open in new window

0
 
Bob LearnedCommented:
That was meant for the GridView.RowDataBound event handler, not for the Repeater.

If e.Row.RowType = DataControlRowType.Footer Then
0
 
patelajkAuthor Commented:
tried doing the code below but get a

Object reference not set to an instance of an object.

on

Dim value As Integer = Val(drv("Amount_Contribution"))


		If e.Row.RowType = DataControlRowType.Footer Then
			Dim grid As GridView = TryCast(sender, GridView)
			Dim sum As Integer = 0
			For Each row As GridViewRow In grid.Rows
				Dim drv As DataRowView = TryCast(row.DataItem, DataRowView)
				Dim value As Integer = Val(drv("Amount_Contribution"))
				sum += value
				e.Row.Cells(1).Text = sum.ToString("#,###")
				e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
				e.Row.Font.Bold = True
			Next row
		End If

Open in new window

0
 
Bob LearnedCommented:
What is the data source for the GridView?
0
 
patelajkAuthor Commented:
its a datareader
0
 
Bob LearnedCommented:
OK, a wrinkle in the plan starts to develop...

I don't like data readers, in this case, because they are forward-only view, and you can't access them by index.  I would think that a DataTable, SqlDataSource, or ObjectDataSource would be a better choice.
0
 
patelajkAuthor Commented:
Thanks
I use this code to create datareader.

And i have never used datareader before, any ideas on how to modify or can i use a dataset instead
0
 
patelajkAuthor Commented:
sorry foget the code
  Public Shared Function ReaderFromArray(ByVal ProcedureName As String, ByVal Parameters As ArrayList) As SqlDataReader
		Dim cn As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        Dim rdr As SqlDataReader
        Dim cmd As New SqlCommand(ProcedureName, cn)
        For Each Param As SqlParameter In Parameters
            cmd.Parameters.Add(Param)
        Next
        cmd.CommandType = CommandType.StoredProcedure
        cn.Open()
        rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        cmd.Dispose()
        Return rdr
		cn.Close()
		rdr.Close()
    End Function

Open in new window

0
 
Bob LearnedCommented:
Try something like this:


    Public Shared Function GetStoredProcedureData(ByVal ProcedureName As String, ByVal Parameters As List(Of SqlParameter)) As DataTable
        Dim connectionString As String = ConfigurationManager.AppSettings("ConnectionString")
        Using adapter As New SqlDataAdapter(ProcedureName, connectionString)
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure
            adapter.SelectCommand.Parameters.AddRange(Parameters.ToArray())
            Dim dt As New DataTable()
            adapter.Fill(dt)
            Return dt
        End Using
    End Function

Open in new window

0
 
Bob LearnedCommented:
If you stored procedure returns multiple record sets, then just change the DataTable to DataSet.
0
 
patelajkAuthor Commented:
sorry

going to need a bit more help

I usuallly pass a arraylist into my original function... not sure what to pass to yours?


	Function GetPortfolioFunds(ByVal intPortfolioID As Integer) As DataSet
		Dim ds As DataSet
		Dim DataTable As DataTable
 
		Dim Params As ArrayList = New ArrayList
		
		Dim sqlParm1 As SqlParameter = New SqlParameter("@PortfolioID", SqlDbType.Int)
		Dim sqlParm2 As SqlParameter = New SqlParameter("@Currency", SqlDbType.VarChar)
		
		sqlParm1.Value = intPortfolioID
		sqlParm2.Value = ddlCurrency.SelectedValue
        
		Params.Add(sqlParm1)
		Params.Add(sqlParm2)
		
		GetStoredProcedureData("sp_g_Secondaries_MYPortfolioFunds", Params)
	
		'	ds = DataAccess.DatasetFromArray("sp_g_Secondaries_MYPortfolioFunds", Params)
 
		Return ds
		ds.Dispose()
		DataTable.Dispose()
	End Function

Open in new window

0
 
Bob LearnedCommented:
Something like this might be better.  Using generics (List(Of)), is much better, because it is strong-typed, and the compiler can catch usage problems, where it can't with the ArrayList, since it accepts and references Object types.


    Private Function GetPortfolioFunds(ByVal portfolioID As Integer, ByVal currency As String) As DataSet
        Dim Params As New List(Of SqlParameter)
 
        Params.Add(New SqlParameter("@PortfolioID", portfolioID))
        Params.Add(New SqlParameter("@Currency", currency))
 
        Return GetStoredProcedureData("sp_g_Secondaries_MYPortfolioFunds", Params)
    End Function

Open in new window

0
 
patelajkAuthor Commented:
thanks..... i got the idea now
thanks for ur help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now