1
 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

2
  • How is your dropdown created. I do believe you have to have a ValueMember set on it if you want it to update the value on the row. I'll try to reproduce this.
    – WozzeC
    Apr 3, 2014 at 14:41
  • DropDown is created on RowDataBound of the gridview..!! Apr 4, 2014 at 3:29

1 Answer 1

0

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)

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 + (GridView1.PageIndex * 10)
          ddlValues.Add(ddl.SelectedValue, ddlIndex)
      End If
  End If
Next

Now page 1 will have values: 0-9

Page 2 will have values 10-19 etc.

2
  • see this will get the values but while repopulating the same value it will return non negative index error.. and i have got the answer from forum Check HEre..!! I have posted the question there too the answer is working fine..! Apr 4, 2014 at 10:21
  • 1
    So you have no issue anymore then. Cool, glad it worked out for you.
    – WozzeC
    Apr 4, 2014 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.