Datagrid.Items not working

CHJM

New member
Joined
Aug 19, 2004
Messages
4
Programming Experience
Beginner
I need to check which rows in a datagrid have been selected. I'm using a checkbox column the users can click on to select a row. My problem is that all the examples for VB .Net contain datagrid.items which when I try to use I'm told "Items is not a member of Datagrid". Even the example in MSDN uses this method.





Example:
' Iterate through each item (row) in the DataGrid control


' and determine whether it is selected.

Dim item As DataGridItem





For Each item In MyGrid.Items
DetermineSelection(item, count)




Next


Am I doing something worng or is this a typo and .Items no longer supported in .Net?
 
actually i havent use that datagriditem object.
i guess your using .net framework 1.1 version 2003.
sorry for me im using 1.0:confused:
but what i've done in determining which item are selected in the datagrid is something like this:
PHP:
  'lets assume that u have a table name dt
  'and it has data bound to it. 
  dim dt as new datatable()
  'put it in the datagrid click event
  dt.Rows(DataGrid1.CurrentCell.RowNumber)(2)<------ it returns the value in column 3 in your datatable.

hope this helps.
 
The problem is I need to select multiple rows. I use a check box column so the user can check off which rows they want to select. I need to be able to loop thru a datagrid to determind which rows have been selected.

According to the MSDN documentation there shoud be a Datagrid.Items container to use in a For Each loop but when I try to code for it, I get the error above.


Thanks for your input though.
 
Here's a code snippet that implements this kind of processing in v1.1.

This code is from a Composite Control I've developed inheriting from DataGrid (thus the 'Me.Items' reference in the 'For Each' statement). What I'm doing here is storing the selected DataKeyField values from the DataGrid's DataKeys collection into a StringCollection (stored in ViewState) so that I can build a SQL Where claus similar to 'WHERE [DataKeyField] IN ('selKey1', 'selKey2', etc....).

Also in my example, the CheckBox was added dynamically at run-time so I need to pull whether it's checked from the incoming Request. 'Me.SelectorCheckBoxFieldName' could be a constant, such as 'cbSelect'.

Hope this helps...

PHP:
Dim skl As StringCollection = Me.SelectionKeyList
For Each item As DataGridItem In Me.Items
Select Case item.ItemType
	 Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem, ListItemType.SelectedItem
	 Dim cbValue As String = HttpContext.Current.Request.Form(item.UniqueID & ":" & Me.SelectorCheckBoxFieldName)
	 Dim cbChecked As Boolean = (Not IsNothing(cbValue)) AndAlso cbValue.ToLower = "on"
	 Dim keyValue As String = Convert.ToString(Me.DataKeys(item.ItemIndex))
	 Dim keyIndex As Integer = skl.IndexOf(keyValue)
	 If cbChecked And keyIndex = -1 Then
		 skl.Add(keyValue)
	 ElseIf (Not cbChecked) And keyIndex > -1 Then
		 skl.RemoveAt(keyIndex)
	 End If
End Select
Next
 
Back
Top