C# Exercises

Home AgriMetSoft About Contact

How to get datagridview selected row in C#

	
namespace get_datagridview_selected_row
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
      var rN = e.RowIndex;
      int col = -1;
      for (int i = 0; i < dataGridView1.Columns.Count; i++)
        if (dataGridView1.Rows[rN].Cells[i].Selected)
          col = i;
    }

    private void fill_row_Click(object sender, EventArgs e)
    {
      dataGridView1.Rows.Add();
      dataGridView1.Rows[0].Cells[0].Value = 1;
      dataGridView1.Rows[0].Cells[1].Value = 11;
      dataGridView1.Rows[0].Cells[2].Value = 111;
      dataGridView1.Rows.Add();
      dataGridView1.Rows[1].Cells[0].Value = 2;
      dataGridView1.Rows[1].Cells[1].Value = 22;
      dataGridView1.Rows[1].Cells[2].Value = 222;
      dataGridView1.Rows.Add();
      dataGridView1.Rows[2].Cells[0].Value = 3;
      dataGridView1.Rows[2].Cells[1].Value = 33;
      dataGridView1.Rows[2].Cells[2].Value = 333;

    }
  }
}
	


Download the project of Visual Studio 2013 in DropBox Download


How to get Datagridview Selected Row in C# | Cell Click Event


List of Exercises