We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Access RowDataBound when Gridview is with Repeater

patelajk
patelajk asked
on
Medium Priority
2,147 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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.

Author

Commented:
any ideas on how i can do.... any sample code would be very helpful
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

Author

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

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
That was meant for the GridView.RowDataBound event handler, not for the Repeater.

If e.Row.RowType = DataControlRowType.Footer Then

Author

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

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What is the data source for the GridView?

Author

Commented:
its a datareader
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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.

Author

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

Author

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

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
If you stored procedure returns multiple record sets, then just change the DataTable to DataSet.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
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

Author

Commented:
thanks..... i got the idea now
thanks for ur help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.