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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Dec 26, 2011, 8:22 PM
Satyapriya NayakPosted Dec 26, 2011, 6:08 AM
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
Datta KharadPosted Dec 26, 2011, 5:57 AM
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();
}
Pravin GhadgePosted Dec 26, 2011, 5:53 AM
Pravin GhadgePosted Dec 26, 2011, 5:51 AM
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();
}