Click any cell of a datagridview its corresponding data will be shown in the textbox

In this blog we will know when we click any cell of a datagridview its corresponding data will be shown in the textbox.

 

 

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

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            bindgrid();

        }

        private void bindgrid()

        {

            OleDbConnection con = new OleDbConnection(ConnectionString);

            con.Open();

            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;

            con.Close();

        }

 

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

        {

          

            txt_sno.Text = "";

            bool FirstValue = true;

            foreach (DataGridViewCell cell in dataGridView1.SelectedCells)

            {

                if (!FirstValue)

                {

                    txt_sno.Text += ", ";

                }

                txt_sno.Text += cell.Value.ToString();

                FirstValue = false;

            }

 

        }

 

     

    }

}

 

 

 

Thanks for reading