Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 WinForm
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;
        }
Posted 18-Sep-12 23:36pm
Edited 18-Sep-12 23:38pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 404
1 Sergey Alexandrovich Kryukov 387
2 Shemeemsha RA 148
3 Abdul Samad KP 125
4 CPallini 100
0 OriginalGriff 6,189
1 Sergey Alexandrovich Kryukov 5,666
2 CPallini 4,810
3 George Jonsson 3,429
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100