C#datagridview分页问题

编程入门 行业动态 更新时间:2024-10-17 05:33:32
本文介绍了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分页问题

本文发布于:2023-11-10 16:21:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!