Hi PRA,
I have created sample code which full-fill your requirement.So please refer the below code and modify as per your requirement.
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.PopulateData();
this.GetData();
}
private void GetData()
{
string constring = @"Data Source=.;Initial Catalog=test;User id = sa;password=sa";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand(" SELECT Id,Fruit FROM FruitTable", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
Comfruit.DataSource = dt;
Comfruit.DisplayMember = "Fruit";
Comfruit.ValueMember = "Id";
}
}
}
}
private void PopulateData()
{
string constring = @"Data Source=.;Initial Catalog=test;User id = sa;password=sa";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT Person.Id,Person.Name,FruitTable.Fruit FROM Person INNER JOIN FruitTable On Person.FruitId=FruitTable.Id", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
}
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
Comfruit.Text = "";
Comfruit.SelectedText = dataGridView1.CurrentRow.Cells[2].Value.ToString();
}
}
Vb.Net
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
Me.PopulateData()
Me.GetData()
End Sub
Private Sub GetData()
Dim constring As String = "Data Source=.;Initial Catalog=test;User id = sa;password=sa"
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand(" SELECT Id,Fruit FROM FruitTable", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
Comfruit.DataSource = dt
Comfruit.DisplayMember = "Fruit"
Comfruit.ValueMember = "Id"
End Using
End Using
End Using
End Sub
Private Sub PopulateData()
Dim constring As String = "Data Source=.;Initial Catalog=test;User id = sa;password=sa"
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT Person.Id,Person.Name,FruitTable.Fruit FROM Person INNER JOIN FruitTable On Person.FruitId=FruitTable.Id", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using ds As New DataSet()
sda.Fill(ds)
dataGridView1.DataSource = ds.Tables(0)
End Using
End Using
End Using
End Using
End Sub
Private Sub dataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs)
textBox1.Text = dataGridView1.CurrentRow.Cells(1).Value.ToString()
Comfruit.Text = ""
Comfruit.SelectedText = dataGridView1.CurrentRow.Cells(2).Value.ToString()
End Sub
End Class