Click here to Skip to main content
16,009,185 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //DataGridViewImageCell cell = (DataGridViewImageCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
            //if (dataGridView1.Columns[e.RowIndex].Name == "Sewagram express")

            //if ((DataTable)(dataGridView2.DataSource).Columns[e.RowIndex].RowName
              //       == "sewagram express")
            DataGridViewImageCell cell = (DataGridViewImageCell) dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (cell.Value=="sewagram express")
            {
                SqlCommand cm1 = new SqlCommand("select * from sewagram", con);
                DataTable dth = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cm1);
                da.Fill(dth);
                dataGridView1.DataSource = dth;
            }
        }
Posted
Updated 22-Feb-14 4:14am
v2
Comments
Krunal Rohit 22-Feb-14 10:14am    
So what is happening ?
-KR
Abhinav Chaudhary 22-Feb-14 10:29am    
sir,when i clicking on "sewagram express" in the datagridview it is not firing the code written in if statement..i have tried all but its not working...please do help.
CHill60 22-Feb-14 13:02pm    
You've declared Cell as DataGridViewImageCell ... DataGridViewCell instead

1 solution

This is a little confusing as you are referencing dataGridView2 with the row and column numbers of where you have clicked in dataGridView1. So assuming you really meant to do that (and that both grids contain the same text "sewagram express"...

In your click event (which is firing ok by the sound of it) you have declared DataGridViewImageCell cell but you want to compare this to a string ... this doesn't sound right to me so I changed that to
DataGridViewCell cell = (DataGridViewCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];

The next problem I hit is with
if (cell.Value=="sewagram express")<br />
which throws the warning
Quote:
Warning 1 Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string' C:\Users\C_2\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 34 17 WindowsFormsApplication1
so I changed that to
if (cell.Value.ToString() == "sewagram express")
and the code ran as expected. This last step wasn't strictly necessary because if you type ? cell.Value.GetType() into the Immediate window you'll find that it is actually a string, but I really don't like leaving Warnings in my code.

I found all of this out by putting a breakpoint in the event and stepped through it taking careful note of what was appearing in my Errors, Output and Locals windows.
 
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