Home » Uncategorized » Pagination in DatGridView C# from sql.

Pagination in DatGridView C# from sql.

public void FillDataGridView1(string search,int page)
        {
            txtPage.Text = (page+1).ToString();
            txtInfo.Text = (page+1).ToString() + " of " +((numberOfSubscriber()/1000)+1).ToString();
            search = "%"+search+"%";
            string query = @"SELECT Top 1000 * FROM Table  where [TableID] like '" + search + "'and [ID] NOT IN (SELECT TOP "+page*1000+" id FROM [Table])";            
            try
            {
                SqlConnection Con = new SqlConnection(connString);
                Con.Open();
                SqlDataAdapter da = new SqlDataAdapter(query, Con);
                System.Data.DataTable dt = new System.Data.DataTable();
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.DataGridView1.Rows.Clear();
                if (ds != null)
                {                    if (ds.Tables[0].Rows.Count >= 1)                    {
                        this.DataGridView1.Rows.Add(ds.Tables[0].Rows.Count);
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            this.DataGridView1.Rows[i].Cells[0].Value = ds.Tables[0].Rows[i][0].ToString(); 
                            this.DataGridView1.Rows[i].Cells[1].Value = ds.Tables[0].Rows[i][1].ToString(); 
                            this.DataGridView1.Rows[i].Cells[2].Value = ds.Tables[0].Rows[i][2].ToString(); 
                            this.DataGridView1.Rows[i].Cells[3].Value = ds.Tables[0].Rows[i][3].ToString(); 
                            this.DataGridView1.Rows[i].Cells[4].Value = ds.Tables[0].Rows[i][4].ToString(); 
                            this.DataGridView1.Rows[i].Cells[5].Value = ds.Tables[0].Rows[i][5].ToString(); 
                            this.DataGridView1.Rows[i].Cells[6].Value = ds.Tables[0].Rows[i][6].ToString(); 
                        }
                    }
                }
                Con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            DataGridView1.ClearSelection();
        }

Leave a Reply

Subscribe

Join 901 other subscribers