Display Selected Row from DataGridView to TextBoxes in Window Application

Step 1: Form

Drag down a DataGrid view and TextBoxes. For this see tutorial6 and tutorial14. From these tutorials you will learn how to insert the data in a database and how to show the database data in DataGridView.



Step 2: Code

Click on the DataGrid View and write this code.
 
 
 

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 First_Csharp_app

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

            SqlConnection con = new SqlConnection("server = MUNESH\\SQL2008R2;Database=data;UID=sa;Password=123;");

            if (e.RowIndex >= 0)

            {

                DataGridViewRow row = this.datagridview1.Rows[e.RowIndex];

                if (cell != null)

                    id_txt.Text = row.Cells["ID"].Value.ToString();

                textBox1.Text = row.Cells["FirsName"].Value.ToString();

                textBox1.Text = row.Cells["LastName"].Value.ToString();

                textBox3.Text = row.Cells["Age"].Value.ToString();

            }

        }

    }

 

Step 3: Output

Run your application.



To learn more, you can go to my blog_munesh.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now