C#datagridview分页问题 [英] C# datagridview paging problem
本文介绍了C#datagridview分页问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我有用于分页datagridview的这段代码,还有一个小问题,因为我还有一个名为ID的文本框,该文本框已绑定,并在datagridview中将绑定源与列ID绑定在一起,但是正如你在代码中看到的,我没有将datgridview与bindsource绑定当我单击任何行时,ID的值确实会更改,该如何克服呢?
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);
//here lies the problem
//ID.DataBindings.Clear();
//ID.DataBindings.Add(new Binding("Text", bind, "ID"));
}
private void CalculateTotalPages()
{
int rowCount = dsData.Tables["DataDetails"].Rows.Count;
this.TotalPage = rowCount / PageSize;
if (rowCount % PageSize > 0) // if remainder is more than zero
{
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); // +
//"order by customerid", con);
}
try
{
// con.Open();
this.daData.SelectCommand = command2;
this.daData.Fill(dt);
}
finally
{
con.Close();
}
return dt;
}
推荐答案
I have managed to overcome this issue in a totally different manner, on datagridview_cellclick event I placed
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
ID.Text = dataGridView1.Rows[i].Cells["ID"].Value.ToString();
and it works when I click row it does what I want, but I now have this problem, when i change selection of rows with up and down arrows the cellclick event doesnt work
这篇关于C#datagridview分页问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文