Hello I have this code for paging datagridview, and a little problem, because I also have textbox called ID which is binded threw binding source with column ID in datagridview, but as you can see in the code I dont bind datgridview with bindingsource and when I click on any row the value of ID doenst change, how to overcome this?
public partial class Form1 : Form
{
SqlConnection con;
private SqlCommand command1;
private SqlCommand command2;
private SqlDataAdapter daData;
DataSet dsData;
BindingSource bind;
private int PageSize = 50;
private int CurrentPageIndex = 1;
private int TotalPage = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=(local);Initial Catalog=FreeDatabase;Integrated Security=True");
command1 = new SqlCommand("Select * from DataDetails order by ID", con);
dsData = new DataSet();
bind = new BindingSource();
daData = new SqlDataAdapter(command1);
daData.Fill(dsData, "DataDetails");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "DataDetails";
bind.DataSource = dsData.Tables["DataDetails"];
this.CalculateTotalPages();
this.dataGridView1.DataSource = GetCurrentRecords(1, con);
}
private void CalculateTotalPages()
{
int rowCount = dsData.Tables["DataDetails"].Rows.Count;
this.TotalPage = rowCount / PageSize;
if (rowCount % PageSize > 0)
{
this.TotalPage += 1;
}
}
private DataTable GetCurrentRecords(int page, SqlConnection con)
{
DataTable dt = new DataTable();
if (page == 1)
{
command2 = new SqlCommand("Select TOP " + PageSize + " * from DataDetails ORDER BY ID", con);
}
else
{
int PreviouspageLimit = (page - 1) * PageSize;
command2 = new SqlCommand("Select TOP " + PageSize +
" * from DataDetails " +
"WHERE ID NOT IN " +
"(Select TOP " + PreviouspageLimit + " ID from DataDetails ORDER BY ID) ", con);
}
try
{
this.daData.SelectCommand = command2;
this.daData.Fill(dt);
}
finally
{
con.Close();
}
return dt;
}