Protected Sub grdView_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles grdView.PageIndexChanging
grdView.SelectedIndex = -1
grdView.PageIndex = e.NewPageIndex
' To persist DDL values at paging using datatable
Dim Data As New DataTable
Data.Columns.Add("RowIndex", Type.GetType("System.Int32"))
Data.Columns.Add("SelectedValue", Type.GetType("System.String"))
For Each Row As GridViewRow In grdView.Rows
Dim ddl As DropDownList = DirectCast(Row.FindControl("ddlSample"), ComboBox)
If ddl.SelectedValue <> "" Then
Dim Rows As DataRow = Data.NewRow
Rows("RowIndex") = Row.RowIndex
Rows("SelectedValue") = ddl.SelectedValue
Data.Rows.Add(Rows)
End If
Next
' Passing the datatable to session
Session("MYDataTable") = Data
grdView.DataSource = grdDataSource
grdView.DataBind()
If Session("MYDataTable") IsNot Nothing AndAlso Session("MYDataTable") IsNot DBNull.Value Then
Data = CType(Session("MYDataTable"), DataTable)
For Each row In Data.Rows
If e.NewPageIndex = row("PageIndex") Then
Dim ddl As DropDownList = DirectCast(grdView.Rows(row("RowIndex")).FindControl("ddlSample"), DropDownList)
ddl.SelectedValue = row("SelectedValue")
End If
Next
End If
The above Code is the code I'm having for Persisting DropDownList value but it is not working as expected.
In my GridView i have two Columns
Details of First column : I have list of employee names
Details of Second Column : It has a dropDownlist in each row with many items in it.
In my grid i have nearly 100 values, So i have set allowPaging="true"
and have set pagesize="10".
The problem what I'm facing is
- Step 1 : I'm selecting a value in dropdownlist of rowindex 2 in page 0
- Step 2 : changing the page to 5, the value I selected in page 0 for the rowindex 2 also appears in the page 5 for the rowindex 2 as per my code.
But what i want is
- Step 1 : I'm selecting a value in dropdownlist of rowindex 2 in page 0
- Step 2 : changing the page to 5, no value should be selected as i haven't selected any values in it.
- Step 3 : I i go back again to Page 0 the value of ddl in Rowindex 2 should have the previously selected value.
EDIT
I'm peristing the Values in Dictionary as mentioned here..!!
Dim ddlValues As New Dictionary(Of String, Integer)
For rowIndex As Integer = 0 To grdOnlineVoter.Rows.Count
Dim ddl As ComboBox = grdView.Rows(rowIndex).FindControl("ddlsample")
If ddl IsNot Nothing Then
If ddl.SelectedIndex > 0 Then
Dim ddlIndex As Integer = rowIndex
ddlValues.Add(ddl.SelectedValue, ddlIndex)
End If
End If
Next
But i'm unable to repopulate it to the Dropdownlist
Alright, based on what you have displayed here I have reached this conclusion.
You are saving the values to the Dictionary to the specific Row number. That is 0-9. However, row numbers does not take pagenumber into consideration. So I recommend you to do the following: (Change is on row 6)
Now page 1 will have values: 0-9
Page 2 will have values 10-19 etc.