This is a fragment of my WebDataGrid:
<ig:WebDataGrid ID="WebDataGrid1" runat="server" > <Columns> <ig:UnboundCheckBoxField Key="SelectedId" > </ig:UnboundCheckBoxField> </Columns> </ig:WebDataGrid>In the postback I want to iterate all the SelectedId column checking what was checked. I was suggested the following code:
For Each row As WebDataGridRow In WebDataGrid1.Rows ' Find the CheckBox control in each row using the field key (assuming "CheckBoxField" is the field key). Dim checkBox As WebDataGridCell = TryCast(row.Items.FindItemByKey("CheckBoxField"), WebDataGridCell) If checkBox IsNot Nothing AndAlso checkBox.Controls.Count > 0 Then Dim checkBoxControl As CheckBox = TryCast(checkBox.Controls(0), CheckBox) If checkBoxControl IsNot Nothing Then ' Check or uncheck the checkbox based on your logic. Dim isChecked As Boolean = checkBoxControl.Checked ' You can now use the 'isChecked' variable as needed. End If End If Next
The problem is the compiler says the types WebDataGridRow and WebDataGridCell are not defined, at least with my version of Infragistics (Infragistics45.Web.v21.2, Version=21.2.20212.9) Am I missing something?
Hello Faustino,
I'm glad I was able to help.
Thank you for choosing Infragistics!
Regards,Ivan Kitanov
Yes: it works only on the grid's PreRender event.
Thank You!
It is possible that the load event of the user control might be early, could you please try using the PreRender event of the grid, to see if the values would be returned as expected?
Please let me know if you need any further assistance.
Regards, Ivan Kitanov
Ivan,
Thanks for your quick response. I tried your code but it is returning all the checkboxes as unchecked, even when I check all by clicking the header's checkbox. This is my code:
Private Function GetSelectedIDs2() As String Dim result As String = "" For Each row In WebDataGrid1.Rows Dim cell As GridRecordItem = row.Items.FindItemByKey("ChkSelectedId") If cell IsNot Nothing Then Dim value = cell.Value Dim isChecked As Boolean = value result &= IIf(isChecked, "1", "0") Else result &= "-" End If Next Return result End Function
I'm calling that function from the Load event of a System.Web.UI.UserControl custom control.
May it be at that point the grid's checkbox data is not ready yet? What event should I handle to get the correct checkboxes' data?
Best regards,
Faustino
The reason why you are getting the errors is that there are no such classes as WebDataGridCell and WebDataGridRow. The correct classes are GridRecordItem- for the cell and GridRecord – for the row. In fact, an easier approach would be to access the value of the unbound field, instead of accessing the checkbox inside and then accessing its checked property. To access the value directly you can use the following code:
For Each row As GridRecord In Me.WebDataGrid1.Rows Dim cell As GridRecordItem = row.Items.FindItemByKey("CheckBoxField") Dim isSelected As Boolean = TryCast(cell.Value, Boolean) ' apply any logic that fits the need of your application Next
Please try this approach and let me know if you have any questions.