DataGridView Selected Row Cell Data To TextBox or Label in C#.Net Window Application

DataGridView Selected Row Cell Data To TextBox or Label


Selected DataGridView Row Cell Data Display To Textbox Or Label Using Window Application C#.Net.


                                             DEMO


                                
                               
                                        C# CODING


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection
                (@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Drizzles\Documents\Visual Studio 2012\WindowsFormsApplication2\WindowsFormsApplication2\Database1.mdf;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from country", con);
            SqlDataAdapter adp=new SqlDataAdapter (cmd);
            DataTable dt=new DataTable ();
            adp.Fill(dt);
            dataGridView1 . DataSource=dt;

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
          
            DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];

          
            lblCountry.Text = row.Cells[0].Value.ToString();
           

            lblCity.Text = row.Cells[1].Value.ToString();

           
        }
    }
}




First - Add New window Form - Add DataBase Table Values





Next - Add The - DataGridView & Labels From ToolBox







Next - Page_Load  Bind The DataBase Values to DataGridView






Next - Double Click DataGridView - Get The Row Cell Values From DataGridview To Pass Labels








Next - Run[F5] - The Program Show The DataBase Values To DataGridView - Click The Particular Row - Cell Values Display The Labels









0 comments:

Post a Comment