|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
/***************************
COPYRIGHT :Ram Krishna Wagle
TITLE :Paging in a Grid View
AUTHOR :Ram Krishna Wagle
DESCRIPTION :To show how a simple paging could be implemented in a grid view
DATE :March 20, 2007
***************************/
namespace PagingGrid
{
public partial class Form1 : Form
{
public int currentIndex = 1; ///Variable to track the paging the gridview
public DataTable mainDTable;
public DataTable tempDTable;
public int numDocs; //no. of documents to be displayed at one screen shot
public int totPages = 1;
public Form1()
{
InitializeComponent();
//LoadGridView();
ColumnForPagingGrid();
LoadGridView();
}
/// <summary>
/// This functions does all the trick of paging.
/// </summary>
public void LoadGridView()
{
if (currentIndex == 1)
{
ColumnForPagingGrid(); //loads the grid structure
//make the connection to the server
string cStr= "Network Library=dbmssocn;Data Source=100.100.100.8,1433;Initial Catalog=dev_main_ors;User ID=sa;Password=sandman";
SqlConnection sConn = new SqlConnection(cStr);
SqlCommand cmdSql = new SqlCommand();
try { sConn.Open(); }
catch (Exception exp) {
MessageBox.Show(exp.ToString());// throw exp;
}
///This is the query that returns the result in bulk to populate the datatable
///Here fire your own query that returns the results that are to be displayed in the grid
string query =
" select fulltext_ref_key as [Doc Key],page_key as [Key]," +
" fulltext_location as [Text Location], [indexed], row_number() over(order by page_key) as [RowNum]" +
" from fulltext_info where db_id=152";
cmdSql = sConn.CreateCommand();
cmdSql.CommandText = query;
SqlDataAdapter sAdp = new SqlDataAdapter(cmdSql);
mainDTable = new DataTable();
sAdp.Fill(mainDTable);
btnL.Enabled = false;
btnR.Enabled = true;
}
int totValues = mainDTable.Rows.Count; //total no. of values that are to be displyed in the paged grid.
//To adjust the no. of entry to be shown in the grid in each view
if (totValues <= 100)
numDocs = 50;
else if (totValues <= 1000 && totValues > 100)
numDocs = 100;
else if (totValues <= 25000 && totValues > 1000)
numDocs = 500;
else
numDocs = 1000;
//this calculation detemines the range of display in one page
int fromValue = numDocs * (currentIndex - 1) + 1;
int toValue = currentIndex * numDocs;
if (toValue > totValues)
toValue = totValues;
if (totValues == 0)
fromValue = 0;
//total no. of pages
totPages = totValues / numDocs;
double exactPages = (double)totValues / numDocs;
if (exactPages > totPages)
totPages += 1;
string dText = "Displaying " + fromValue + " to " + toValue + " Records of " + totValues + " Records";
if (totValues > 0)
{
dispText.Text = dText;
}
//enables or disables the left and right button according to the current state of current index
if (totValues < numDocs)
{
btnL.Enabled = false;
btnR.Enabled = false;
}
else if (currentIndex * numDocs > totValues)
{
btnR.Enabled = false;
btnL.Enabled = true;
}
else if (currentIndex > 1 && currentIndex * numDocs < totValues)
{
btnL.Enabled = true;
btnR.Enabled = true;
}
string expression = "RowNum >=" + fromValue + " and RowNum <=" + toValue;
tempDTable = new DataTable();
tempDTable = mainDTable.Clone();
foreach (DataRow dr in mainDTable.Select(expression))
tempDTable.ImportRow(dr);
dgvPagingGrid.Rows.Clear();
for (int i = 0; i < tempDTable.Rows.Count; i++)
{
DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
cell1.Value = tempDTable.Rows[i]["indexed"].ToString();
DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
cell2.Value = tempDTable.Rows[i]["Doc Key"].ToString();
DataGridViewTextBoxCell cell3 = new DataGridViewTextBoxCell();
cell3.Value = tempDTable.Rows[i]["Key"].ToString();
DataGridViewTextBoxCell cell4 = new DataGridViewTextBoxCell();
cell4.Value = tempDTable.Rows[i]["Text Location"].ToString();
DataGridViewRow newRow = new DataGridViewRow();
newRow.Cells.Add(cell1);
newRow.Cells.Add(cell2);
newRow.Cells.Add(cell3);
newRow.Cells.Add(cell4);
dgvPagingGrid.Rows.Add(newRow);
}
dgvPagingGrid.Columns["Indexed"].Width = 60;
dgvPagingGrid.Columns["Doc Key"].Width =
Math.Max(100, dgvPagingGrid.Columns["Doc Key"].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true));
dgvPagingGrid.Columns["Key"].Width =
dgvPagingGrid.Columns["Key"].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
dgvPagingGrid.Columns["Text Location"].MinimumWidth =
dgvPagingGrid.Columns["Text Location"].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
dgvPagingGrid.Columns["Text Location"].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill;
}
/// <summary>
/// Displays the format for the grid view with empty rows for the required columns
/// </summary>
private void ColumnForPagingGrid()
{
dgvPagingGrid.Columns.Clear();
DataGridViewImageColumn iconView = new DataGridViewImageColumn();
iconView.DefaultCellStyle.NullValue = null;
iconView.Name = "Indexed";
iconView.HeaderText = "Indexed";
dgvPagingGrid.Columns.Clear();
dgvPagingGrid.Columns.Add(iconView);
dgvPagingGrid.Columns.Add("Doc Key", "Doc Key");
dgvPagingGrid.Columns.Add("Key", "Key");
dgvPagingGrid.Columns.Add("Text Location", "Text Location");
dgvPagingGrid.Columns["Indexed"].Width = 60;
dgvPagingGrid.Columns["Doc Key"].Width =
Math.Max(100, dgvPagingGrid.Columns["Doc Key"].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true));
dgvPagingGrid.Columns["Key"].Width =
dgvPagingGrid.Columns["Key"].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
dgvPagingGrid.Columns["Text Location"].MinimumWidth =
dgvPagingGrid.Columns["Text Location"].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
dgvPagingGrid.Columns["Text Location"].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill;
}
private void btnR_Click(object sender, EventArgs e)
{
currentIndex += 1;
LoadGridView();
}
private void btnL_Click(object sender, EventArgs e)
{
currentIndex -= 1;
LoadGridView();
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.