C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

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


Posted Date: 23 May 2009    Resource Type: Code Snippets    Category: DataGridView
Author: Syed Shakeer Hussain Member Level: Diamond    
Rating: 1 out of 5Points: 10 (Rs 5)



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();
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
DataGridview(Windows Appliction)  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Disable CheckBoxes in a CheckBox Column of DataGridView Control
Previous Resource: DataGridView with Row Numbers
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use