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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Values in a Created Datagrid 1

Status
Not open for further replies.

ac11nyc

Programmer
Oct 1, 2003
94
US
I have a datagrid created programtically(dgAccounts) inside another datagrid(dgHousehold). I have to step through dgB in order to get certain values but then i have to step through dgA in order to get some values from there also.

this is the one i created programtically:
Public Sub dgHousehold_OnItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Try
'When each row is created in the DataGrid, eval the ItemType
If ((e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem)) Then
'If the ItemType is Item or AlternatingItem,
'Create a new DataGrid object named OrdersDataGrid
Dim dgAccounts As DataGrid = New DataGrid
dgAccounts.DataKeyField = "HouseholdId"

'Format the Datagrid to look how you want it
With dgAccounts
.BorderWidth = Unit.Pixel(1)
.CellPadding = 4
.CellSpacing = 0
.BorderColor = Color.SteelBlue
.Width = Unit.Pixel(300)

.ItemStyle.Font.Name = "Arial"
.ItemStyle.Font.Size = FontUnit.XXSmall

.AlternatingItemStyle.BackColor = Color.LightGray

.ShowHeader = True
.HeaderStyle.BackColor = Color.SteelBlue
.HeaderStyle.ForeColor = Color.White
.HeaderStyle.Font.Bold = True
.HeaderStyle.Font.Size = FontUnit.XXSmall

.AutoGenerateColumns = False

.DataKeyField = "AccountNumber"
End With

'*******Add a series of BoundColumns
'*****AccountNumber*******
Dim bc As BoundColumn = New BoundColumn
'Set the BoundColumn Values
bc.HeaderText = "Account Number"
bc.DataField = "AccountNumber"
bc.ItemStyle.Wrap = False
'Add the BoundColumn to the dgAccounts
dgAccounts.Columns.Add(bc)

'*****InceptionDate*******
bc = New BoundColumn
bc.HeaderText = "Inception Date"
bc.DataField = "InceptionDate"
bc.DataFormatString = "{0:d}"
bc.ItemStyle.Wrap = False
dgAccounts.Columns.Add(bc)
'*****End BoundColumns*****

'Get the Accounts DataView
Dim _accounts As DataView = ds.Tables("Accounts").DefaultView
_accounts.RowFilter = "HouseholdId='" + e.Item.Cells(0).Text + "'"

'Bind the Grid
dgAccounts.DataSource = _accounts
dgAccounts.DataBind()

'Add the dgAccounts to dgHousehold
e.Item.Cells(2).Controls.Add(dgAccounts)

My Question: How do i access the values inside this Datagrid so that i can enter them into the database. Any help will be greatly appreciated. Rest of the code looks like this:

Private Sub cmdEnroll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnroll.Click
Try
Dim strFC As String = ddlFC.SelectedItem.Value
Dim dgItem As DataGridItem
For Each dgItem In dgHousehold.Items
Dim iHouseholdId As Integer = dgHousehold.DataKeys(dgItem.ItemIndex)
>>THIS IS WHERE I WANT TO PUT THE CODE TO STEP THROUGH THE CREATED DATAGRID<<
Next
Catch ex As Exception
lblMessage.Text = "Enrolling: " + ex.Message
End Try
End Sub
 
Try this (its untested but gives you an example)

Code:
Dim i as Integer = 0
For Each dgItem in dgHousehold.Items
i = 0
Dim oGrid as DataGrid
oGrid = CType(dgItem.FindControl("dgAccounts"), DataGrid)

While (i < oGrid.Items.Count)
  Dim oLabel as Label
  oLabel = CType(oGrid.Items(i).FindControl("myLabel"), Label)
  '' Or find whatever controls you need

  ''Do your database insert

  
  i++

Loop

Next
 
I had something like that earlier but as soon as it tries to access dgAccounts, it returns "Object reference not set to an instance of an object.
 
For some reason i cant access anything inside the nested datagrid. How can i access any info in the nested grid??
 
All my code is up top. Only thing i added was:

Dim dgAccounts As DataGrid = CType(dgItem.FindControl("dgAccounts"), DataGrid)
>>i = dgAccounts.Items.Count >>Wont allow this because i get back "Object reference not set to an instance of an object.
 
Try using NEW:
Dim dgAccounts As NEW DataGrid = CType(dgItem.FindControl("dgAccounts"), DataGrid)
 
jbenson,

thanx but didnt work either. It is still telling me that "object refernce not set to an instance of an object." It just does not want to access the nested grid whatsoever. What am i doing wrong??
 
Yes it does render and it has the correct information
 
Here is a quick example I did. I have a master dg, dgMaster, and a nested dg, dgClients:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim dgClients As New DataGrid
   Dim dgi As DataGridItem
   Dim i As Integer

   For i = 0 To dgMaster.Items.Count - 1
      dgClients = dgMaster.Items(i).Cells(1).FindControl("dgClients")
      For Each dgi In dgClients.Items
         Response.Write(dgi.Cells(1).Text + "<BR>")
      Next
      Response.Write("<BR>")

   Next i

End Sub

You just need to change the names to fit your object names. It should get you going in the right direction.
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top