Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I've got a DropDownList and a DataGrid on my Webform and I've got the following code:

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'Put user code to initialize the page here
       If Not Me.IsPostBack Then
           Me.SqlDataAdapter1.Fill(Me.DataSet11)
           Me.DataGrid1.DataBind()
           Me.DropDownList1.SelectedIndex = 0
       End If
   End Sub

   Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
       Me.DataView1.RowFilter = "Substring(FirstName,1,1) = '" & Me.DropDownList1.SelectedItem.Value & "'"
   End Sub

 

 

My DropDownList has A,B,C,D, etc as items. Whenever I select a new item from my DropDownList I want my DataGrid to be updated accordingly.

 

It is not happening. I also chaned my RowFilter to LIKE instead of Substring. It also works fine on Window forms. It is the Web form that is giving me this problem. What am I doing wrong?

:confused:

Posted

I got different results

 

Her is the code I modified:

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'Put user code to initialize the page here
       If Not Me.IsPostBack Then
           Me.SqlDataAdapter1.Fill(Me.DataSet11)
           Dim rf As String = "Substring(FirstName,1,1) ='" & Me.DropDownList1.SelectedItem.Value & "'"
           Me.DataView1.RowFilter = rf
           With Me.DataGrid1
               .DataBind()
               .AllowSorting = True
           End With
           Me.DropDownList1.SelectedIndex = 0
           Me.Cache("ds2") = Me.DataSet11.Copy
       Else
           Me.DataSet11.Clear()
           Me.DataSet11 = Me.Cache("ds2")
       End If
   End Sub

   Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
       Dim rf As String = "Substring(FirstName,1,1) ='" & Me.DropDownList1.SelectedItem.Value & "'"
       Me.DataView1.RowFilter = rf
       With Me.DataGrid1
           .DataBind()
           .AllowSorting = True
       End With

 

I suspected that during postback the values are not being retained in my DataSet. So i cached the value and when I put the debugger on, I could see that the count for DataSet (DataSet.tblStudents.count) was now a non-zero value (telling me that the DataSet value are being retained). The RowFilter value, works the first time and the grid is properly displayed with the FirsName filter working as I want it to.

 

However, if I select a different value in my DropDownList, the RowFilter fails. and the DataView's count is 0. Telling me there is nothing selected in the DataView.

Posted

Here is what I found

 

This is the code that seems to wrking. Though I don't know if it is th right way to do it.

 

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'Put user code to initialize the page here
       If Not Me.IsPostBack Then
           Me.SqlDataAdapter1.Fill(Me.DataSet11)
           Me.Cache("ds") = Me.DataSet11.Copy
           Me.Cache("dv") = Me.DataView1
           Dim rf As String = "Substring(FirstName,1,1) ='" & Me.DropDownList1.SelectedItem.Value & "'"
           Me.DataView1.RowFilter = rf
           With Me.DataGrid1
               .DataBind()
               .AllowSorting = True
           End With
           Me.DropDownList1.SelectedIndex = 0
       Else
           Me.DataView1 = Me.Cache("dv")
           Me.DataSet11 = Me.Cache("ds")
       End If
   End Sub

   Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
       Dim rf As String = "Substring(FirstName,1,1) ='" & Me.DropDownList1.SelectedItem.Value & "'"
       Me.DataView1.RowFilter = rf
       With Me.DataGrid1
           .DataSource = Me.DataView1
           .DataBind()
           .AllowSorting = True
       End With
   End Sub

 

As you can see, I am caching the DataView object too.

I wonder how it can be impemented with a TreeView control.:D

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...