Resources » Code Snippets » DataGridView

How to get the Values of Slected Cell Row DataGridview(Windows Appliction) using C#?


Posted Date: 23-May-2009  Last Updated:   Category: DataGridView    
Author: Member Level: Gold    Points: 10



How to get the Values of Slected Cell Row DataGridview(Windows Appliction) using C#?

Open Window Form.Add on DataGridview and three Textboxes on windows Form.
In the above example I am created on emp table and having 3 columns as id number,empname(varchar (20),salary number(20).
In form load Retrive the data a emp Table and fill in the Datagridview.When you click or Select a Row on DataGridview the particular selected row values will display in Textboxes by using below code.

i = dataGridView1.SelectedCells[0].RowIndex;
textBox1.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();

before using this code you have to double click your DataGridview and write the code in
between the

Private void dataGridView1_CellContentClick_(objectsender,DataGridViewCellEventArgs e)
{

}


Complete coding is below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlDataAdapter da;
DataSet ds;
int i;
SqlConnection conn;
private void Form1_Load(object sender, EventArgs e)
{
conn = new SqlConnection("connetion tring");
conn.Open();
da= new SqlDataAdapter("select * from emp", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "emp");
dataGridView1.DataSource = ds.Tables["emp"];
}

Private void dataGridView1_CellContentClick_1(objectsender,DataGridViewCellEventArgs e)
{
i = dataGridView1.SelectedCells[0].RowIndex;
textBox1.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
}
}



Did you like this resource? Share it with your friends and show your love!

Responses to "How to get the Values of Slected Cell Row DataGridview(Windows Appliction) using C#?"
Author: chinnari    01 Jul 2010Member Level: Silver   Points : 1
I wrote the same code but this event is not firing. Is there is delegate we need to attach



Author: Syed Shakeer Hussain    19 Jul 2010Member Level: Gold   Points : 1
Hi Sukanya,
It will works.
just double click on your Gridview and use the above code,
you have to click on the text,then it will work



Author: Vishal Kadam    02 Mar 2011Member Level: Bronze   Points : 0
Thanks Syed Shakeer Hussain.

i was really searching for same...



Author: kingfisher    15 Mar 2011Member Level: Gold   Points : 1
also you can use the following line of code on the
doubleclick event of the "dataGridView1"

dataGridView1.Item("SrNo", dataGridView1.CurrentRow.Index).Value



Guest Author: Uday Shah     04 May 2012
i have need the how transfer the record from datagridview to textbox between two window form c#.net and sql server 2005 window application. my requirement when we click the datagrid cell of row then according to ID transfer record in other form in textbox.
plz plz if anybody know then help me help me



Guest Author: kirti     16 Jul 2012
its not working....plz tell some other idea to retrieve the value of selected row in text box.....


Author: rashmikc    22 Jul 2012Member Level: Bronze   Points : 0
Ya its working


Guest Author: mahmoud elgamal     01 Aug 2012
thxxxx


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Kamal Sharma
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India