EDIT : I just realised I provide the wrong link regarding the same problem I have. Now the link is the right one.
I have the same problem as this question that created 5 years ago Aspxcheckbox object not set to an instance of an object (Devexpress aspxgridview). However, the question does not have any answer that I'm looking for.
To explain my problem, I have AspxGridview. In that gridview I have a AspxCheckboxes. My gridview page size is 20, so it will have 20 rows each page (if the data have more than 20, if not then gridview only have 1 page). The error only happened when I have more than 1 page, meaning at 21 item. So let say I click row 4, 5, and 10. Then, I click the btnTransferAsset_Click
function, the loop will start to run okay but once it reached row 21, the error will come out saying Object reference not set to an instance of an object
. What is the meaning of this ?
The markup look like this :
<dx:ASPxGridView ID="grdAsset" runat="server" Width="100%" AutoGenerateColumns="true" Caption="Assets List" KeyFieldName="id" DataSourceID="getAssets">
<Columns>
<dx:GridViewDataColumn Caption="" FieldName="id" VisibleIndex="0" Visible="false" ReadOnly="true" />
<dx:GridViewDataButtonEditColumn Caption="#" VisibleIndex="1" Width="50px">
<DataItemTemplate>
<dx:ASPxCheckBox ID="chkID" runat="server" />
</DataItemTemplate>
</dx:GridViewDataButtonEditColumn>
<dx:GridViewDataColumn Caption="" FieldName="asset_id" VisibleIndex="2" />
<dx:GridViewDataColumn Caption="Asset Category" FieldName="category_name" VisibleIndex="3" />
<dx:GridViewDataColumn Caption="Asset Brand Name" FieldName="brand_name" VisibleIndex="4" />
'others Datacolumn.....
</Columns>
<SettingsBehavior AllowSelectByRowClick="true" ColumnResizeMode="Control" />
<SettingsPager Position="Bottom" PageSize="20">
<PageSizeItemSettings Items="10, 20, 50, 100, 200, 500" ShowAllItem="True" Visible="True" />
</SettingsPager>
<Settings ShowFilterRow="true" ShowFooter="true" />
<SettingsDataSecurity AllowDelete="False" AllowEdit="False" AllowInsert="False" />
<Settings GridLines="Horizontal" />
<Styles>
<AlternatingRow Enabled="True" />
</Styles>
</dx:ASPxGridView>
<asp:ObjectDataSource ID="getAssets" runat="server" TypeName="Asset" SelectMethod="selectAsset">
<SelectParameters>
'the parameters...
</SelectParameters>
</asp:ObjectDataSource>
<br/>
<dx:ASPxButton ID="btnTransferAsset" runat="server" Text="Transfer Assets" Font-Bold="true" Visible="false" OnClick="btnTransferAsset_Click" />
This the code behind :
Private Sub btnTransferAsset_Click (sender As Object, e As EventArgs)
Dim dt As New DataTable
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("id"), New DataColumn("asset_id"), New DataColumn("category_name")})
For rowIndex As Integer = 0 To grdAsset.VisibleRowCount - 1
Dim column As GridViewDataButtonEditColumn = CType(grdAsset.Columns(1), GridViewDataButtonEditColumn)
Dim cb As ASPxCheckBox = CType(grdAsset.FindRowCellTemplateControl(rowIndex, column, "chkID"), ASPxCheckBox)
If cb.Checked Then 'got error here, at the loop 21 times, it shows "Object reference not set to an instance of an object."
Dim id As Integer = CType(grdAsset.GetRowValues(rowIndex, New String() {"id"}), Integer)
Dim asset_id As String = CType(grdAsset.GetRowValues(rowIndex, New String() {"asset_id"}), String)
Dim category_name As String = CType(grdAsset.GetRowValues(rowIndex, New String() {"category_name"}), String)
dt.Rows.Add(id, asset_id, category_name)
End If
Next
'my others code...
End Sub
CodePudding user response:
I have solved the problem. I changed the AspxCheckbox in DataItemTemplate to GridViewCommandColumn.
From this :
<dx:GridViewDataButtonEditColumn Caption="#" VisibleIndex="1" Width="50px">
<DataItemTemplate>
<dx:ASPxCheckBox ID="chkID" runat="server" />
</DataItemTemplate>
</dx:GridViewDataButtonEditColumn>
To this :
<dx:GridViewCommandColumn ShowSelectCheckbox="true" Width="50px" VisibleIndex="1" />
Then my code behind also changed like this :
Private Sub btnTransferAsset_Click (sender As Object, e As EventArgs)
Dim dt As New DataTable
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("id"), New DataColumn("asset_id"), New DataColumn("category_name")})
For i As Integer = 0 To grdAsset.VisibleRowCount - 1
If grdAsset.Selection.IsRowSelected(i) Then
Dim id As Integer = CType(grdAsset.GetRowValues(i, New String() {"id"}), Integer)
Dim asset_id As String = CType(grdAsset.GetRowValues(i, New String() {"asset_id"}), String)
Dim category_name As String = CType(grdAsset.GetRowValues(i, New String() {"category_name"}), String)
dt.Rows.Add(id, asset_id, category_name)
End If
Next
'my others code...
End Sub
CodePudding user response:
Full disclosure, I am not an expert on GridViews. Hopefully you will get a real answer from an expert. However, you may need to add code to handle paging. Take a look at the following link. It has code that shows page handling. It is in C# but there are a number of free sites that can convert it to vb.net if needed.