C# .NET - Pagin for GridView

Asked By Mohammad Ajmal Yazdani on 28-Nov-06 12:17 AM

Hello,

can anyone send me code that how can I done "Pagin" for GridView.....

i m devloping a window application with C#


regards,

ajmal

Paging a Windows Forms DataGridView or DataGrid (the example is with a DataGridView)

K Pravin Kumar Reddy replied to Mohammad Ajmal Yazdani on 28-Nov-06 12:45 AM

hello

using System.Data.SqlClient;
using System.Text;
using System.ComponentModel;
public class Form1
{
 SqlDataAdapter da;
 SqlConnection conn;
 DataSet ds = new DataSet();

 private void Form1_Load(object sender, System.EventArgs e)
 {
   string strConn;
   SqlCommand cmd;
   StringBuilder sbCmd = new StringBuilder();
   strConn = string.Format("Server = {0};", Environment.MachineName);
   strConn += "Database = NorthWind; Integrated Security = SSPI;";
   conn = new SqlConnection(strConn);
   cmd = new SqlCommand("Select count(ProductName) From Products", conn);
   try {
     da = new SqlDataAdapter("Select * from Products", conn);
     conn.Open();
     nuPage.Maximum = Math.Ceiling(cmd.ExecuteScalar / 10);
     nuPage.Minimum = 1;
     nuPage.Increment = 1;
     nuPage.Value = 1;
     conn.Close();
     da.Fill(ds, 0, 10, "Products");
     ds.Tables("Products").DefaultView.AllowNew = false;
     DataGridView1.DataSource = ds.Tables("Products");
     foreach (object col in DataGridView1.Columns) {
       if (col is DataGridViewCheckBoxColumn) {
         ((DataGridViewCheckBoxColumn)(col)).Visible = false;
       } else if (col is DataGridViewTextBoxColumn) {
         DataGridViewTextBoxColumn tbc = ((DataGridViewTextBoxColumn)(col));
         if (tbc.Name == "ProductName") {
           tbc.Width = 275;
           tbc.HeaderText = "Product Name";
         } else if (tbc.Name == "UnitPrice") {
           tbc.Width = 75;
           tbc.HeaderText = "Price";
           tbc.DefaultCellStyle.Format = "c";
           tbc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
         } else {
           tbc.Visible = false;
         }
       }
     }
   } catch (Exception ex) {
     Trace.WriteLine(ex.ToString);
   }
 }

 private void nuPage_ValueChanged(object sender, System.EventArgs e)
 {
   int intStart = (nuPage.Value - 1) * 10;
   ds.Clear();
   da.Fill(ds, intStart, 10, "Products");
 }
}

 

reference

http://www.vb-tips.com/dbPages.aspx?ID=3785fb7f-69ce-4089-8a35-b1ec09f63071

gagan kopparam replied to Mohammad Ajmal Yazdani on 28-Nov-06 12:51 AM

<%@ Page Language="VB" Debug="False" Strict="True" Explicit="True" Buffer="True"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Dynamic Column Sorting and Paging in ASP.NET</title>
</head>
<body bgcolor="#FFFFFF" topmargin="0" marginheight="0">
<script language="VB" runat="server">
Sub Page_Load (sender As Object, e As EventArgs)
    If SortExp.Text = "" then
        BindSQL ("title asc")
    Else
        BindSQL (SortExp.Text)
    End If
End Sub

Sub BindSQL (SortField As String)
    Dim MyConnection As SqlConnection
    Dim MyCommand As SqlDataAdapter
    Dim DS as Dataset
    Dim sqlStr As String = "SELECT authors.au_fname, authors.au_lname, titles.title, titles.price FROM authors, titleauthor, titles WHERE titleauthor.au_id = authors.au_id AND titleauthor.title_id = titles.title_id ORDER BY title asc"
    Dim strConn As String = "server=(local);uid=sa;pwd=;database=pubs;Trusted_Connection=yes;"
    Dim RcdCount As Integer
    SortExp.Text = SortField
    MyConnection = New SqlConnection(strConn)
    MyCommand = New SqlDataAdapter(sqlStr, MyConnection)
    DS = new Dataset()
    MyCommand.Fill(DS, "pubs")
    Dim Source As DataView = DS.Tables(0).DefaultView
    Source.Sort = SortField
    Pubs.DataSource = Source
    Pubs.Databind()
End Sub

Function SortOrder (Field As String) As String
    Dim so As String = SortExp.Text
    If Field = so Then
        SortOrder = Replace (Field,"asc","desc")
    Else
        SortOrder = Replace (Field,"desc","asc")
    End If
End Function

Sub BookList_Sort (Sender As Object, E As DataGridSortCommandEventArgs)
    pubs.CurrentPageIndex = 0 'To sort from top
    BindSQL (SortOrder (E.SortExpression).ToString()) 'Rebind our Datagrid
End Sub
Sub BookList_PageChange (Source As Object, E As DataGridPageChangedEventArgs)
    pubs.CurrentPageIndex = E.NewPageIndex
    BindSQL (SortExp.Text)
End Sub
</script>
<BR><BR>
<H2>Dynamic Column Sorting and Paging in ASP.NET</H2>
<BR><BR>
<form runat="server">
    <ASP:Datagrid id="Pubs" runat="server"
    Pagesize="10"
    AllowSorting="True"
    AllowPaging="True"
    AllowCustomPaging="False"
    PagerStyle-Visible = "True"
    PagerStyle-Mode = "NumericPages"
    HeaderStyle-BackColor="Blue"
    HeaderStyle-ForeColor="White"
    OnSortCommand="BookList_Sort"
    OnPageIndexChanged="BookList_PageChange"
    AutoGenerateColumns="false"
    >
    <Columns>
        <asp:BoundColumn HeaderText="Title" SortExpression="title asc" DataField="title" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
        <asp:BoundColumn HeaderText="Last Name" SortExpression="au_lname asc" DataField="au_lname" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
        <asp:BoundColumn HeaderText="First Name" SortExpression="au_fname asc" DataField="au_fname" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
        <asp:BoundColumn HeaderText="Price" SortExpression="price asc" DataField="price" Headerstyle-Horizontalalign="Center" ItemStyle-Wrap="false"/>
    </Columns>
    </ASP:DataGrid>
    <asp:Label id="SortExp" runat="server" Visible="False" />
</form>
</body>
</html>

------

Pagesize="10"
AllowPaging="True"
AllowCustomPaging="False"
PagerStyle-Visible = "True"
PagerStyle-Mode = "NumericPages"
OnPageIndexChanged="BookList_PageChange"

-----

 Sub BookList_PageChange (Source As Object, E As DataGridPageChangedEventArgs)
        pubs.CurrentPageIndex = E.NewPageIndex 'Set datagrid to current page index
        BindSQL (SortExp.Text) ' Rebind Datagrid with sort order
    End Sub

- G Kopparam

wht is nuPage

Mohammad Ajmal Yazdani replied to K Pravin Kumar Reddy on 28-Nov-06 01:04 AM

kevin,

can u tell me wht is "nuPage"?

Regards,

ajmal

i m taking abt Windows Application......
Mohammad Ajmal Yazdani replied to gagan kopparam on 28-Nov-06 01:14 AM

hello,

i m taking abt Windows Application......

thnx a lot!

check out another example here
K Pravin Kumar Reddy replied to Mohammad Ajmal Yazdani on 28-Nov-06 01:25 AM

hello check out one more example here

DataGrid Paging - C# Windows Forms

https://secure.codeproject.com/useritems/DataGridPaging.asp

Mohammad Ajmal Yazdani replied to K Pravin Kumar Reddy on 28-Nov-06 01:53 AM

hello kevin,

i got "nuPage" is "NumericUpDown"......

Its works perfectly......

Now I want Paging and Sorting both for GridView.....

Is there any solution......

regards,

ajmal

Customize Sorting in the Windows Forms DataGridView Control
K Pravin Kumar Reddy replied to Mohammad Ajmal Yazdani on 28-Nov-06 02:06 AM

hello

The http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx control provides automatic sorting but, depending on your needs, you might need to customize sort operations. For example, you can use programmatic sorting to create an alternate user interface (UI). Alternatively, you can handle the http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.sortcompare.aspx event or call the Sort(IComparer) overload of the http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.sort.aspx method for greater sorting flexibility, such as sorting multiple columns.

The following code examples demonstrate these three approaches to custom sorting. For more information, see http://msdn2.microsoft.com/en-us/library/95scxcdy.aspx.

check example here

http://msdn2.microsoft.com/en-us/library/ms171608.aspx

Datagrid Windows control lacks paging
sundar k replied to Mohammad Ajmal Yazdani on 28-Nov-06 02:44 AM

please check the below article from MSDN, but they have given a sample code to implement the paging, please have a look into this,

"how to perform paging with Datagrid Windows Control by using Vb.NET"

http://support.microsoft.com/kb/305271