Blog

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

Posted by Satyapriya Nayak Blogs | Windows Forms C# Jun 30, 2012
In this blog we will know when we 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

post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.