How to retrive datagridveiw view record into textbox of windows form in C#
I am new in programming of C#. So pls send the ans of this questions because it is too difficult to retrive records from datagridview to txtbox of windows form.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pravin GhadgeRank #1,340 · 1.3K reputationPosted Dec 26, 2011, 12:51 AM EST
For achiving ur task u have to create event:
For eg:
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
Textbox1.Text=dataGridView1.CurrentRow.Cells[0].Value.ToString();
}
Pravin GhadgeRank #1,340 · 1.3K reputationPosted Dec 26, 2011, 12:53 AM EST
Datta KharadRank #2,135 · 668 reputationPosted Dec 26, 2011, 12:57 AM EST
You can use RowHeadermouseclick event. After filling datagridview just click the row which you want to display into txtbox and then that record will display in txtbox. See following example:-
private void dataGridView2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
textBox7.Text = dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString(); //Cells["Columnname or Col index"]
textBox8.Text = dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString(); //Rows[e.RowIndex] -> Selected row
textBox9.Text = dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();
}
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 26, 2011, 1:08 AM EST
Try this...
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_bind
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oda;
DataSet ds;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bind();
}
void bind()
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from customer";
com = new OleDbCommand(str, con);
oda = new OleDbDataAdapter(com);
ds = new DataSet();
oda.Fill(ds, "customer");
dataGridView1.DataMember = "customer";
dataGridView1.DataSource = ds;
con.Close();
}
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
}
}
}
Thanks
If this post helps you mark it as answer
Sam HobbsRank #107 · 20.5K reputationPosted Dec 26, 2011, 3:22 PM EST