how to use get selected row event of griedview in windows app

Posted by Shanky11 under VB.NET on 3/3/2013 | Points: 10 | Views : 1305 | Status : [Member] | Replies : 1
i have a grieview i want that if i select any row after that edit button is there outside the griedview on click of this button i want to fill txtboxes with selected rows of griedview values on that form which opened on click btn


Posted by: Satyapriyanayak on: 3/4/2013 [Member] [MVP] Silver | Points: 25

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Datagridview_select_textbox_data
public partial class Form1 : Form
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
string str;

DataSet ds;
public Form1()

private void Form1_Load(object sender, EventArgs e)
private void bindgrid()
OleDbConnection con = new OleDbConnection(ConnectionString);
str = "select * from employee";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "employee");
dataGridView1.DataMember = "employee";
dataGridView1.DataSource = ds;

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
txt_sno.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
txt_name.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
txt_address.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
txt_age.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();

DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
txt_sno.Text = row.Cells[0].Value.ToString();
txt_name.Text = row.Cells[1].Value.ToString();
txt_address.Text = row.Cells[2].Value.ToString();
txt_age.Text = row.Cells[3].Value.ToString();

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
txt_sno.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
txt_name.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
txt_address.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
txt_age.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();

If this post helps you mark it as answer

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response