DropDownList和DataGrid结合使用时出错!(100分相送)
DropDownList用于根据人名进行筛选,Datagrid用于显示筛选的结果。DropDownList的第一值为"全部",代表显示所有人员信息,其余值为一个一个的人名。在执行下列代码时出错,出错部分已经标出,请问如何解决此问题?
Private Sub drpSelXM_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drpSelXM.SelectedIndexChanged
If Me.drpSelXM.SelectedItem.Value = 0 Then
'未筛选,在Datagrid中显示所有人员信息
Me.BindData()
Else
'已筛选,在Datagrid中显示选中的人员信息
Me.BindDataByFilter()
End If
End Sub
'----------------------------------------------------------
'数据绑定
Private Sub BindData()
Dim cn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim strSql As String
cn = New OleDbConnection(ConfigurationSettings.AppSettings("cnstr"))
'选择所有人员
strSql = "select xm from table"
da = New OleDbDataAdapter(strSql, cn)
ds = New DataSet()
da.Fill(ds, "renyuan")
Me.DataGrid1.DataSource = ds.Tables("rypxjl").DefaultView
Me.DataGrid1.DataBind()
cn.Close()
End Sub
'----------------------------------------------------------
'选择筛选条件后的数据绑定
Private Sub BindDataByFilter()
Dim cn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim strSql As String
cn = New OleDbConnection(ConfigurationSettings.AppSettings("cnstr"))
'选择特定的人员
strSql = "select xm from table where " & Me.drpSelXM.SelectedItem.Value
da = New OleDbDataAdapter(strSql, cn)
ds = New DataSet()
da.Fill(ds, "renyuan")
Me.DataGrid1.DataSource = ds.Tables("rypxjl").DefaultView
'----------------------------
'----------------------------
'----------------------------
'----------------------------
'执行下面这条语句出错:(
'错误信息为:Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
'我想可能是下列情况导致出错
'假定上一次筛选返回的记录有5页(即pagecount=5)并且用户停留在第五页上(即currentPageIndex=4),而根据本次筛选的条件产生的记录只有1页
Me.DataGrid1.DataBind()
cn.Close()
End Sub