Skip to content

How to navigate to a record in Datagridview

Madito Kevin

Asked Apr 6, 2015, 1:58 AM EDT3 answers
Share
LinkedInX
I want to navigate to a record in Datagridview by selecting/click on a record without using a button click.
and display a record number,or current record number. 
Please help here is my code snippet.
 
 
C#
private void labelUpdate()
        {


            if (hwdg.SelectedRows.Count>=inc)
            {
                hwdg.FirstDisplayedScrollingRowIndex = inc;
                hwdg.Rows[inc].Selected = true;
               //inc++;
                label27.Text = "Record" + (inc + 1) + " of " + ds2.Tables["tblComp"].Rows.Count;
            }
            else if(hwdg.SelectedRows.Count<=inc)
            {
                hwdg.Rows[inc - 1].Selected = true;
                label27.Text = "Record" + (inc + 1) + " of " + ds2.Tables["tblComp"].Rows.Count;
            }
        }

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    This code didn't work when i try to navigate nothing happen im navigating through the Datagridview to scroll through the records
    0
  • 0
    can anyone help me im trying to navigate through a datagridview,when i select a record from a datagridview i want to see its record number.
    0
  • -1
    Try this example to your need its will work without problem. I do use this and its works. Any question feelfree to ask.
    Listen, this example use a MS ACCESS database.

    public partial class Facturacao : Form
        {
            public Frm()
            {   
                InitializeComponent();
            }

            OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\table\App_Data\yourtable.mdb;Persist Security Info=False");
            OleDbCommand comm;

            string connstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\table\App_Data\yourtable.mdb;Persist Security Info=False";

    TypeScript
    private void dvgFrm_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0)
                {
    
    
                    DataGridViewRow row = this.dvgFrm.Rows[e.RowIndex];
                    if (dvgFrm != null)
    
    
                        textBox1.Text = row.Cells["name"].Value.ToString();
                        textBox2.Text = row.Cells["passportenumber"].Value.ToString();
                }
            }


    -1