PDA

View Full Version : سوال: خطا در صفحه بندی Datagridview



hosseines
چهارشنبه 09 تیر 1395, 23:01 عصر
سلام خدمت دوستان
من تقریبا 50 تا رکورد در بانک اطلاعاتی دارم که میخواهم اطلاعات را بصورت صفحه بندی در دیتاگرید نمایش دهم هر صفحه 10 رکورد ولی بازدن دکمه صفحه بعدی خطا میده.


private OleDbCommand cmd1;
private OleDbCommand cmd2;
private OleDbDataAdapter ada1;
DataSet ds;
static OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb;Persist Security Info=True");
//--------------------------------------
private int pageSize = 10;
private int currentpageindex = 0;
private int Totalpage = 0;


private void Form1_Load(object sender, EventArgs e)
{
cmd1 = new OleDbCommand("select * from Tbl_Personel order by CodePersenol", con);
ds = new DataSet();
ada1 = new OleDbDataAdapter(cmd1);
ada1.Fill(ds, "Tbl_Personel");
DWG1.DataSource = ds;
DWG1.DataMember = "Tbl_Personel";
this.calculateTotalpages();
this.DWG1.ReadOnly = true;
this.DWG1.DataSource = Getcurrentrecords(1, con);
txtNumRecord.Text = (currentpageindex + 1) + " از " + Totalpage;
}

public void calculateTotalpages()
{
int rowCount = ds.Tables["Tbl_Personel"].Rows.Count;
this.Totalpage = rowCount / pageSize;
if (rowCount % pageSize > 0)
{
this.Totalpage += 1;
}
}
private DataTable Getcurrentrecords(int page, OleDbConnection con)
{
DataTable dt = new DataTable();
if (page == 1)
{
cmd2 = new OleDbCommand(" select Top " + pageSize + " * from Tbl_Personel order by CodePersenol", con);
//InitializePageNavigationButtons();
}
else
{
int previouspagelimit = (page - 1) * pageSize;
cmd2 = new OleDbCommand(" select Top " + pageSize + " * from Tbl_Personel " + " CodePersenol not in " +
"(select Top " + previouspagelimit + " CodePersenol from Tbl_Personel order by CodePersenol)", con);
}

try
{
this.ada1.SelectCommand = cmd2;
this.ada1.Fill(dt);
}
finally
{
con.Close();
}
return dt;
}

private void btnNext_Click(object sender, EventArgs e)
{
if (this.currentpageindex < this.Totalpage)
{
this.currentpageindex++;
this.DWG1.DataSource = Getcurrentrecords(this.currentpageindex, con);
txtNumRecord.Text = currentpageindex + " از " + Totalpage;
}
}

private void btnend_Click(object sender, EventArgs e)
{
this.currentpageindex = this.Totalpage;
txtNumRecord.Text = currentpageindex + " از " + Totalpage;
this.DWG1.DataSource = Getcurrentrecords(this.currentpageindex, con);
}

private void btnback_Click(object sender, EventArgs e)
{
if (this.currentpageindex > 1)
{
this.currentpageindex--;
this.DWG1.DataSource = Getcurrentrecords(this.currentpageindex, con);
txtNumRecord.Text = currentpageindex + " از " + Totalpage;
}
}

private void btnHome_Click(object sender, EventArgs e)
{
this.currentpageindex = 1;
this.DWG1.DataSource = Getcurrentrecords(this.currentpageindex, con);
txtNumRecord.Text = currentpageindex + " از " + Totalpage;
}



کجا اشتباه کردم...؟

Mahmoud.Afrad
پنج شنبه 10 تیر 1395, 01:19 صبح
در کوئری کلمه where رو ننوشتید.
شما کل رکوردها را در دیتاست ds لود کردید و این اشتباه است(صفحه بندی علاوه بر دسته بندی رکوردها باعث میشه فقط رکوردهای مورد نیاز لود بشوند نه همه رکوردها). در کوئری ، توسط فانکشن Count تعداد رکوردها رو میتونید بدست بیارید.
private OleDbConnection _con;
private OleDbCommand _cmd;
private OleDbDataAdapter _adapter;
//--------------------------------------
private int _pageSize = 10;
private int _currentPageIndex;
private decimal _totalpage;

public Form1()
{
InitializeComponent();

_con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Application.StartupPath + "\\Database1.mdb;Persist Security Info=True");
_cmd = new OleDbCommand { Connection = _con };
_adapter = new OleDbDataAdapter(_cmd);

dataGridView1.DataSource = GetPageRecords(1);
}


private DataTable GetPageRecords(int pageIndex)
{
DataTable dt = new DataTable();
try
{
_totalpage = CalculateTotalPageCount();

if (pageIndex == 1)
{
_cmd.CommandText = " select Top " + _pageSize + " * from Tbl_Personel order by CodePersenol";
//InitializePageNavigationButtons();
}
else
{
int previousPageRecordlimit = (pageIndex - 1) * _pageSize;
_cmd.CommandText = "select Top " + _pageSize + " * from Tbl_Personel WHERE CodePersenol not in " +
" (select Top " + previousPageRecordlimit +
" CodePersenol from Tbl_Personel order by CodePersenol)" +
" order by CodePersenol";
}
_adapter.Fill(dt);
_currentPageIndex = pageIndex;
}
catch (Exception ex)
{
//do what you want
}
finally
{
_con.Close();
}
return dt;
}

private decimal GetRecordCount()
{
_cmd.CommandText = "select COUNT(CodePersenol) from Tbl_Personel";
if (_cmd.Connection.State != ConnectionState.Open)
_cmd.Connection.Open();
object objPageCount = _cmd.ExecuteScalar();
decimal totalRecordCount = objPageCount == null ? 0 : Convert.ToDecimal(objPageCount);
return totalRecordCount;
}

private decimal CalculateTotalPageCount()
{
decimal recordCount = GetRecordCount();
decimal pageCount = Math.Ceiling(recordCount/_pageSize);
return pageCount;
}
کانکشن رو مطابق نیازتون اصلاح کنید.