Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing items in the datagrid from code behind 2

Status
Not open for further replies.

belle9

Programmer
Nov 3, 2004
87
US
Here is my item in the html:

Code:
<asp:BoundColumn DataField="AthleteName" HeaderText="Athlete Name">
														<HeaderStyle CssClass="dgHeader"></HeaderStyle>
														<ItemStyle Wrap="False"></ItemStyle>
													</asp:BoundColumn>

I'd like to set AthleteName to be FirstName and LastName concatenated. I grab those values in the code behind, on the page load, like this:

Code:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           BindDataGrid(lblMsg, dgAthletes, conString, "admin_GetallAthletes", "", "")
         End Sub

Public Sub BindDataGrid(ByVal lbl As Label, ByVal dg As DataGrid, ByVal cs As String, ByVal sp As String, ByVal p As String, ByVal pv As String)
        Dim oConn As SqlConnection
        Dim myCommand As SqlCommand

        oConn = New SqlConnection(cs)
        oConn.Open()
        myCommand = New SqlCommand(sp, oConn)
        myCommand.CommandType = CommandType.StoredProcedure
        If p <> "" Then
            myCommand.Parameters.Add(New SqlParameter(p, SqlDbType.Int))
            myCommand.Parameters(p).Value = pv
        End If
        dg.DataSource = myCommand.ExecuteReader
        dg.DataBind()
        oConn.Close()


    End Sub

I'd like to manipulate the two values that are returned and set them to be the AthleteName datagrid item...
 
Just return them concatenated from your stored procedure

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Sometime you need to grab things in the datagrid while they are being painted.

This example does this.

<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="True"
ShowFooter="True">

</asp:datagrid>

Private Totals(100) As Double '100 is more than the expected number of staff members

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound


If e.Item.ItemType = ListItemType.AlternatingItem Or _
e.Item.ItemType = ListItemType.Item Then
For i As Integer = 0 To e.Item.Cells.Count - 1
Dim v As Double = CDbl("0" & e.Item.Cells(i).Text)
Totals(i) = Totals(i) + v
Next
ElseIf e.Item.ItemType = ListItemType.Footer Then
For i As Integer = 0 To e.Item.Cells.Count - 1
e.Item.Cells(i).Text = Totals(i)
Next
End If
End Sub
 
Thanks both of you...I'll use both your answers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top