I have a GridView that shows particular data list or reports when clicking Generate button.
When I'm moving to the next page of the GridView or when I click the next page, the GridView binding becomes empty and it shows no GridView list.
Here's my code in .aspx
<asp:GridView ID="GridView1"
runat="server"
CellPadding="4"
HeaderStyle-ForeColor="White"
ForeColor="#333333"
GridLines="None"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt"
RowStyle-CssClass="row"
AllowSorting="True"
AllowPaging="True"
PageSize="25" Visible="false" OnRowDataBound="GridView1_RowDataBound"
EnableViewState="false">
<EmptyDataTemplate>
No record found.
</EmptyDataTemplate>
</asp:GridView>
Here's my code for Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
userid = IIf(SessionHandler.UserID = "", User.Identity.Name.ToUpper, SessionHandler.UserID)
V3Substance.Attributes.Add("onfocus", "javascript:if (this.value=='Substance Cas Number') { this.value = ''; }")
V3Substance.Attributes.Add("onblur", "javascript:if (this.value=='') { this.value = 'Substance Cas Number'; }")
If Not Page.IsPostBack Then
V1Division.DataSource = GetDivision()
V1Division.DataBind()
V1BR.Items.Clear()
V1BR.DataSource = GetBusinessRule(V1Division.Text)
V1BR.DataBind()
MultiView1.ActiveViewIndex = 0
Panel1.DefaultButton = "V1Generate"
End If
End Sub
Here's my code for GridView1_PageIndexChanging
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
Dim dt As DataTable = TryCast(GridView1.DataSource, DataTable)
GridView1.DataSource = SortTable(dt)
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind()
End Sub
And here's my code for BindGridView
Private Sub BindGridview(ByVal query As String, ByVal countquery As String)
ViewState.Add("Query", query)
ViewState.Add("CountQuery", countquery)
Dim count As Integer = 0
If Description.Text = "1" Then
ValidateFamily(query)
Else
DataSource = dbHelper.GetRecordDT(query)
End If
count = DataSource.Rows.Count
'GridView1.DataSource = Nothing
'GridView1.DataBind()
'GridView1.PageIndex = 0
GridView1.DataSource = DataSource
GridView1.Visible = True
GridView1.DataBind()
ExportToExcel.Visible = IIf(count < 1, False, True)
Panel1.Visible = IIf(count < 1, False, True)
SetTotalResult(count)
End Sub
I hope you can help me with this. I can't seem to know what could be the problem. I tried different approach to this and find a solutions from the internet but it didn't help. Thanks a lot.
You should be using
GridView1.DataSource = DataSource
It's unclear from your snippet what DataSource
is, but you use it in the BindGridview
method also.
Start using different names for variables, naming them exactly as properties is very confusing and will lead to errors.
Update
Dim source As SqlDataSource
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
source = New SqlDataSource
source.ID = "mySource"
source.SelectCommand = "select * from table"
source.ConnectionString = connStr
If Not Page.IsPostBack Then
GridView1.DataSource = source
GridView1.DataBind
End If
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
GridView1.DataSource = source
GridView1.PageIndex = e.NewPageIndex
GridView1.DataBind
End Sub