Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Building a Gridview in code
Message
 
 
To
15/10/2009 19:26:20
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01429565
Message ID:
01429591
Views:
37
>Having resolved my earlier problems with Gridview, I'm now trying to advance to building one in code. I dropped the control on the form and set the paging and sorting properties to TRUE. The ASP declaration is
>< asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
>    AllowPaging="True" AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging">
>< /asp:GridView>
>To build the gridview I put the following in the Page_Load method
>    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
>        If GridView1.Columns().Count = 0 Then
>            Dim strConn As String = "SERVER=myServer;USER ID=myId;PASSWORD=myPassword;DATABASE=myDB;"
>            Dim objConn As New MySqlConnection(strConn)
>            Dim strSql As String
>
>            Dim objGvDS As New DataSet
>
>            strSql = "select * from myTable"
>            Dim daParms As New MySqlDataAdapter(strSql, objConn)
>            daParms.Fill(objGvDS, "dtParms")
>        
>            Dim myCol1 As New BoundField
>            myCol1.HeaderText = "FirstHeader"
>            myCol1.DataField = "FirstField"
>            GridView1.Columns.Add(myCol1)
>
>            Dim myCol2 As New BoundField
>            myCol2.HeaderText = "SecondHeader"
>            myCol2.DataField = "SecondField"
>            GridView1.Columns.Add(myCol2)
>
>            GridView1.DataSource = objGvDS
>            GridView1.DataBind()
>        
>            objConn.Close()
>            objConn.Dispose()
>        End If
>        
>    End Sub
>
>    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object,_
>                                                                        ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
>        sender.pageindex = e.NewPageIndex
>    End Sub
>
>As in my question about the Treeview control, the statement
If GridView1.Columns().Count = 0 Then
was because each time the Page_Load method fired, it would add two columns to the gridview.
>
>The problem at the moment, however, is that clicking on a new page number seems to regenerate the entire dataset and always brings back Page 1.
>
>Is there a better place to put this code that Page_Load and where should I be putting the code to set the page number?
>
>As always.........all replies muchly appreciated...........Rich

Check for Page.IsPostBack in the Load method. Also, why do you want to build the grid in code and not visually?
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform