Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my sql database i have this table
tblEmployees
EmplName Dept Designation salary
John Smith IT Research Research Analyst 23000.00
John Michael IT Operations Manager 25000.00
Will Smith IT Support Manager 13000.00
Lames Brown IT Support Manager 1000.00
Will Emi Support Sales 14000.00
Sam Diva IT Support Manager Assistant 17000.00

In my winForms, i two datagridviews (dgv1 & dgv2). The EmplName column is displayed (showing all names)in dgv1. Now i want to display the columns (Dept, Designation, salary, etc..) in the second datagridview (dgv2) IF a cell / row Content is clicked. And this shd apply for all EmplNames from dgv1 when selected or clicked.(dynamic)

Thatz what i have so far:


//dgv1 cellcontentclick event
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Connection string
string C = (@"Data Source=…… ");
SqlConnection con = new SqlConnection(C);

// TSQL sp to choose tables dynamic
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("select * from " + selected cellcontent"");

SqlDataAdapter ada = new SqlDataAdapter(cmd);

try
{
// Open connection
con.Open();

DataTable dt = new DataTable();
ada.Fill(dt);
//dgv2
dataGridView2.AutoGenerateColumns = true;
dataGridView2.DataSource = dt;

}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
Posted
Updated 6-Nov-14 22:31pm
v2
Comments
Laiju k 7-Nov-14 5:57am    
what is your issue
mikybrain1 7-Nov-14 5:59am    
Where don't u understand? Data from one gridview if selected to another
Laiju k 7-Nov-14 6:17am    
Have you written the query,are you having problem with binding ,gridview1 selectedindexchanging
mikybrain1 7-Nov-14 6:42am    
Yes i have written the query which populates my dgv1 with just one column (EmplName) from the table in the database. Now i want to populate the second dgv2 with the entire colums (Dept, Designation and salary) if the cell with the EmplName is clicked.

I have tried this codes BUT it's showing me the same record in the dgv1

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}

listBox1.Items.Add(dataGridView1.Rows[e.RowIndex].Cells["EmplName"].Value.ToString());

I'm expecting a result like this:
John Smith IT Research Research Analyst 23000.00
Is it possible?
Laiju k 7-Nov-14 6:46am    
as you already said you are binding dgv1 with just one column (EmplName) from the table in the database.then how can you get that result

1 solution

C#
protected void dataGridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    //Accessing BoundField Column
    string name = GridView1.SelectedRow.Cells[0].Text;

    //Accessing TemplateField Column controls
    string country = (GridView1.SelectedRow.FindControl("lblCountry") as Label).Text;

    lblValues.Text = "<b>Name:</b> " + name + " <b>Country:</b> " + country;
}


can you try like this.event is there is events part in properties
 
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