Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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 MyDBApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }
        SqlConnection con = new SqlConnection("Server=127.0.0.1;Database=SampleDB;User id=sa;password=sa2008;");

        
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                FillData();
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            
            using (SqlCommand cmd = new SqlCommand("Insert into Emp_Details   (Emp_ID,Name,Education) values (" + textBox2.Text + ",'" + textBox3.Text + "','" + textBox1.Text + "')", con))
            {
                cmd.ExecuteNonQuery();
            }
            FillData();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    
    
     void FillData()
        {
            
                using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Emp_Details", con))
                {
                    DataTable t = new DataTable();
                    a.Fill(t);
                    dataGridView1.DataSource = t;
                }
            
        }
    }
}


Using this code i can able to enter the data in textboxes, it will automatically populated in grid view.
Now i want when i click on particular row in grid view the data must populate in text boxes.


Can any one help me to solve this issue.

Thanks.
Posted

try this:-
textBox1.Text = dataGridView1.CurrentRow.Cells["Column name"].Value.ToString();

or
textBox1.Text = dataGridView1.CurrentRow.Cells[Column index].Value.ToString();


use CellClick event of dataGridView1 and try the above code:
C#
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
   textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value ;
}
 
Share this answer
 
v3
Comments
Karthik Achari 13-Feb-14 3:07am    
it won't work.

Thanks for your support
TrushnaK 13-Feb-14 4:39am    
now try this... cell click event.
Try like this
C#
textBox.Text = dataGridView.SelectedRows(0).Cells(cellIndex).Value.ToString()

Refer:
how to get gridview selected row value to text box which is in another windows form[^]
Hope this may help.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900