Click here to Skip to main content
15,922,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell cell = (DataGridViewCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];

            if (cell.ValueType.ToString() == "Abhinav")
            {
                MessageBox.Show("Right Name");
            }
            else
            {
                MessageBox.Show("Wrong Name");
            }
        }
Posted
Comments
Sergey Alexandrovich Kryukov 24-Feb-14 13:05pm    
What "table"? What is the problem?
—SA

Was putting this in a comment but it's easier as a solution ...
Further to SA's solution (which I do not disagree with in any way!)

Are you getting confused between "Columns" and "Cells"?

If a DataGridView has 2 "columns" then in the click event you can capture which "column" was clicked ... it is the Cell index on the row, and is passed in as e.ColumnIndex.

You can still access the other "columns" in the datagrid in the same event, you just have to know their column (Cell) index ...

For example, you could click on any column in the row but check if the first column matches the text ... using your code sample this would look like
C#
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell cell = (DataGridViewCell)dataGridView2.Rows[e.RowIndex].Cells[0];

            if (cell.ValueType.ToString() == "Abhinav")
            {
                MessageBox.Show("Right Name");
            }
            else
            {
                MessageBox.Show("Wrong Name");
            }
        }
 
Share this answer
 
Comments
Abhinav Chaudhary 25-Feb-14 8:27am    
Thank you sir it is working now
You cannot "fire" this event, you only can handle it. In .NET, you cannot invoke any event in any class/structure except from the code of the type where the event is declared. This is a .NET fool-proof feature for events; in this aspects, events are different from "regular" delegate instances.

So, in this respect, the question simply makes no sense. Also, the handling of the event you are asking about it totally irrelevant to the number of columns in anything. Maybe, you have some real problem, but you did not describe any.

—SA
 
Share this answer
 
Comments
Abhinav Chaudhary 25-Feb-14 8:26am    
Thank you!it is working now
Sergey Alexandrovich Kryukov 25-Feb-14 8:55am    
Great. You are very welcome.
—SA

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