Index Out of Bounds Error

G

Guest

Hi,

I have a datagrid in my application that has a Template Column with a
radiobutton. After selecting the item in the datagrid and the submit button,
the program executes getSelectedItem(). See below. This works fine except
when there is only one row in the datagrid. Then I get the Index out of
bounds error.

Has anyone seen this before?

Dave
Private Function GetSelectedItems(ByVal grdlst As DataGrid) As String
Dim rowCount As Integer = 0
Dim gridSelections As StringBuilder = New StringBuilder
Try
Dim oDataGridItem As DataGridItem
For Each oDataGridItem In grdlst.Items
Dim rdoSelected As RadioButton =
CType(oDataGridItem.Cells(0).Controls(1), RadioButton)

If rdoSelected.Checked = True Then
rowCount += 1
gridSelections.AppendFormat("{0}~",
grdlst.DataKeys(oDataGridItem.ItemIndex).ToString())
End If
Next

Catch ex As Exception
lblMessage.Text = "Error finding selected row; " & ex.Message
End Try

If rowCount > 0 Then
'Remove the last separation symbol
'gridSelections.Remove(gridSelections.Length - 1, 1)
Return (gridSelections.ToString)
Else
Return ("")
End If
End Function
 
K

Ken Cox [Microsoft MVP]

Just wondering if your problem has to do with the For Each loop trying to
find a checkbox inside header and footer items?

A fix would be to check that you are only looking inside
ListItemType.AlternatingItem and ListItemType.Item types

Ken
MVP [ASP.NET]
 
G

Guest

Ken,

Thanks for the response. I found the problem. I didn't define a DataKey
for the Datagrid. DataKeys are required to pick up the selected row. So my
code looks like the following now:
dvPromote = New DataView(dtPromote)
dgRequests.DataSource = dvPromote
dgRequests.DataKeyField = "Tran_ID"
dgRequests.DataBind()
I didn't show this code before, but the dgRequests.DataKeyField entry was
missing.

Thanks again for your help.

Dave

Ken Cox said:
Just wondering if your problem has to do with the For Each loop trying to
find a checkbox inside header and footer items?

A fix would be to check that you are only looking inside
ListItemType.AlternatingItem and ListItemType.Item types

Ken
MVP [ASP.NET]



Hi,

I have a datagrid in my application that has a Template Column with a
radiobutton. After selecting the item in the datagrid and the submit
button,
the program executes getSelectedItem(). See below. This works fine
except
when there is only one row in the datagrid. Then I get the Index out of
bounds error.

Has anyone seen this before?

Dave
Private Function GetSelectedItems(ByVal grdlst As DataGrid) As String
Dim rowCount As Integer = 0
Dim gridSelections As StringBuilder = New StringBuilder
Try
Dim oDataGridItem As DataGridItem
For Each oDataGridItem In grdlst.Items
Dim rdoSelected As RadioButton =
CType(oDataGridItem.Cells(0).Controls(1), RadioButton)

If rdoSelected.Checked = True Then
rowCount += 1
gridSelections.AppendFormat("{0}~",
grdlst.DataKeys(oDataGridItem.ItemIndex).ToString())
End If
Next

Catch ex As Exception
lblMessage.Text = "Error finding selected row; " & ex.Message
End Try

If rowCount > 0 Then
'Remove the last separation symbol
'gridSelections.Remove(gridSelections.Length - 1, 1)
Return (gridSelections.ToString)
Else
Return ("")
End If
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top