C# - Navigate Rows In DataGridView

This code snippet allows you to navigate through a DataGridView control. The navigation buttons select a row and the details button gets the values for the currently selected row and displays it in a message box.
  1. // Place a datagridview control on the form
  2. // Place five button controls on the form and name them as:
  3. // FirstRecord, PreviousRecord, NextRecord, LastRecord, Details
  4.  
  5.     public partial class Form1 : Form
  6.     {
  7.         private int RowIndex = 0;
  8.  
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.  
  14.         private void Form1_Load(object sender, EventArgs e)
  15.         {
  16.             dataGridView1.MultiSelect = false;
  17.             dataGridView1.Columns.Add("FirstName", "First Name");
  18.             dataGridView1.Columns.Add("LastName", "Last Name");
  19.             dataGridView1.Columns.Add("Email", "Email");
  20.  
  21.             object[] row1 = { "John", "Doe", "john.doe@mail.xzy.com"};
  22.             object[] row2 = { "Tom", "Baker", "tom.baker@mail.xzy.com"};
  23.             object[] row3 = { "Suzan", "Carter", "suzan.carter@mail.xzy.com"};
  24.  
  25.             dataGridView1.Rows.Add(row1);
  26.             dataGridView1.Rows.Add(row2);
  27.             dataGridView1.Rows.Add(row3);
  28.         }
  29.  
  30.         private void FirstRecord_Click(object sender, EventArgs e)
  31.         {
  32.             RowIndex = 0;
  33.             dataGridView1.Rows[RowIndex].Selected = true;
  34.         }
  35.  
  36.         private void PreviousRecord_Click(object sender, EventArgs e)
  37.         {
  38.             if (RowIndex >0)
  39.             {
  40.                 RowIndex--;
  41.                 dataGridView1.Rows[RowIndex].Selected = true;
  42.             }
  43.         }
  44.  
  45.         private void NextRecord_Click(object sender, EventArgs e)
  46.         {
  47.             int TotalRows = dataGridView1.Rows.Count - 1;
  48.             if (RowIndex < TotalRows)
  49.             {
  50.                 RowIndex++;
  51.                 dataGridView1.Rows[RowIndex].Selected = true;
  52.             }
  53.         }
  54.  
  55.         private void LastRecord_Click(object sender, EventArgs e)
  56.         {
  57.             RowIndex = dataGridView1.Rows.Count-1;
  58.             dataGridView1.Rows[RowIndex].Selected = true;
  59.         }
  60.  
  61.         private void Details_Click(object sender, EventArgs e)
  62.         {
  63.             DataGridViewSelectedRowCollection RowCollection = dataGridView1.SelectedRows;
  64.            
  65.             foreach ( DataGridViewRow Row in RowCollection)
  66.             {
  67.                 string FirstName = Row.Cells[0].Value.ToString();
  68.                 string LastName = Row.Cells[1].Value.ToString();
  69.                 string Email = Row.Cells[2].Value.ToString();
  70.  
  71.                 MessageBox.Show(String.Format("First Name: {0} \nLast Name: {1} \nEmail: {2}",FirstName, LastName, Email));
  72.             }
  73.         }
  74.     }

1 comment:

Post a Comment