Click here to Skip to main content
15,896,387 members

C# datagridview paging problem

shonezi asked:

Open original thread
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?
C#
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;
        }
Tags: C# (C# 4.0), Windows Forms

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900