如何通过gridview标题中的复选框检查所有复选框 [英] How I can check all checkbox by a checkbox in header in gridview
问题描述
当我选中gridview标题中的复选框时,检查所有复选框都在gridview的所有页面中自动...
此错误<< 对象引用未设置为对象的实例。 >>
此行的
when i checked the checkbox in the header of gridview checked all checkboxes auto in all pages in gridview ...
this error <<Object reference not set to an instance of an object. >>
for this line
dt = ds.Tables(0)
我尝试过:
受保护的子CheckAll_CheckedChanged( ByVal sender As Object,ByVal e As System.EventArgs)
Dim dt As DataTable = Session(LoadData)
Dim ds As DataSet
dt = ds.Tables(0)
Session(LoadData)= ds.Tables(tabel1)
For i = 0 to dt.Rows.Count - 1
如果CType(GridNatRel.Rows(i).FindControl(CheckAll),CheckBox)。检查然后
ElseIf dt.Rows(i).Item( check1)= 1然后
否则
dt.Rows(i).Item(check1)= 0
结束如果
下一页
CHECK1()
结束次级
<<这是子CHECK1()>>
受保护的子CHECK1()
Dim dt As DataTable = Session(LoadData)
Dim startIndex As Integer = GridNatRel.PageIndex * GridNatRel.PageSize
For x = 0 To GridNatRel.Rows.Count - 1
If Convert.IsDBNull(dt.Rows( startIndex + x)。Item(check1))OrElse
CInt(dt.Rows(startIndex + x).Item(check1))= 1然后
'如果dt.Rows(startIndex + x).Item(check1)= 1那么
CType(GridNatRel.Rows(x).FindControl(CheckBox1) ),CheckBox).Checked = True
Else
CType(GridNatRel.Rows(x).FindControl(CheckBox1),CheckBox).Checked = False
结束如果
下一页
结束Sub
What I have tried:
Protected Sub CheckAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim dt As DataTable = Session("LoadData")
Dim ds As DataSet
dt = ds.Tables(0)
Session("LoadData") = ds.Tables("tabel1")
For i = 0 To dt.Rows.Count - 1
If CType(GridNatRel.Rows(i).FindControl("CheckAll"), CheckBox).Checked Then
ElseIf dt.Rows(i).Item("check1") = 1 Then
Else
dt.Rows(i).Item("check1") = 0
End If
Next
CHECK1()
End Sub
<< and this is sub CHECK1() >>
Protected Sub CHECK1()
Dim dt As DataTable = Session("LoadData")
Dim startIndex As Integer = GridNatRel.PageIndex * GridNatRel.PageSize
For x = 0 To GridNatRel.Rows.Count - 1
If Convert.IsDBNull(dt.Rows(startIndex + x).Item("check1")) OrElse
CInt(dt.Rows(startIndex + x).Item("check1")) = 1 Then
'If dt.Rows(startIndex + x).Item("check1") = 1 Then
CType(GridNatRel.Rows(x).FindControl("CheckBox1"), CheckBox).Checked = True
Else
CType(GridNatRel.Rows(x).FindControl("CheckBox1"), CheckBox).Checked = False
End If
Next
End Sub
推荐答案
查看您的代码段问题:
Look at this code segment from your question:
Dim dt As DataTable = Session("LoadData")
Dim ds As DataSet
dt = ds.Tables(0)
Session("LoadData") = ds.Tables("tabel1")
您已声明 dt
并为其指定了值...
You have declared dt
and assigned it a value...
Dim dt As DataTable = Session("LoadData")
但是 ds
你只是声明它
Dim ds As DataSet
你实际上并没有给它一个值甚至是s ay
You don't actually give it a value or even say
ds = New DataSet()
所以 ds
目前 Nothing
。它没有桌子。所以,当您点击
So ds
is currently Nothing
. It has no tables. So when you hit the line
dt = ds.Tables(0)
时,您将收到错误
对象引用未设置为对象的实例。
Object reference not set to an instance of an object.
您没有使用 ds
代码中的任何其他位置。所以只需删除行
You are not using ds
anywhere else in your code. So just remove the lines
Dim ds As DataSet
dt = ds.Tables(0)
那将是
That is going to leave you with
Dim dt As DataTable = Session("LoadData")
Session("LoadData") = ds.Tables("tabel1")
显然会因为我们摆脱 ds
而失败,所以你需要摆脱
Obviously that will fail because we got rid of ds
so you need to get rid of
Session("LoadData") = ds.Tables("tabel1")
同样。
另一种选择是实际编写一些代码那个人口tes ds
这篇关于如何通过gridview标题中的复选框检查所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!