Hi Amol111,
Is your question for window application ?
If no refer the below article.
Else refer the below code.
Form1.cs
public static string customerId;
public static string contactName;
public static string country;
public Form1()
{
InitializeComponent();
BindGrid();
}
private void BindGrid()
{
string constring = @"Data Source=.;Initial Catalog=Northwind;User id = sa;password=test";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
//Set AutoGenerateColumns False
dataGridView1.AutoGenerateColumns = false;
//Set Columns Count
dataGridView1.ColumnCount = 3;
//Add Columns
dataGridView1.Columns[0].Name = "CustomerId";
dataGridView1.Columns[0].HeaderText = "Customer Id";
dataGridView1.Columns[0].DataPropertyName = "CustomerID";
dataGridView1.Columns[1].HeaderText = "Contact Name";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].DataPropertyName = "ContactName";
dataGridView1.Columns[2].Name = "Country";
dataGridView1.Columns[2].HeaderText = "Country";
dataGridView1.Columns[2].DataPropertyName = "Country";
dataGridView1.DataSource = ds.Tables[0];
}
}
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
customerId = dataGridView1.CurrentRow.Cells[0].Value.ToString();
contactName = dataGridView1.CurrentRow.Cells[1].Value.ToString();
country = dataGridView1.CurrentRow.Cells[2].Value.ToString();
Popup pop = new Popup();
pop.Show();
}
Popup.cs
public Popup()
{
InitializeComponent();
}
private void Popup_Load(object sender, EventArgs e)
{
lblCustomerId.Text = Form1.customerId;
lblContactName.Text = Form1.contactName;
lblCountry.Text = Form1.country;
}
Screenshot