当表中有多个列时,如何在datagridview上触发click事件? [英] how to fire click event on datagridview when there are more than one columns in table?

查看:74
本文介绍了当表中有多个列时,如何在datagridview上触发click事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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");
            }
        }

推荐答案

将此作为评论但是作为解决方案更容易......

继续SA的解决方案(我不以任何方式不同意!)



你是否在列和细胞之间感到困惑?



如果DataGridView有2个列,那么在点击事件中你可以捕获点击了哪个列......它是行上的Cell索引,并以 e.ColumnIndex 传入。



您仍然可以在同一事件中访问数据网格中的其他列,您只需要知道它们的列(Cell)索引...



例如,您可以单击行中的任何列,但检查第一列是否与文本匹配...使用您的代码示例,这看起来像

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
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");
            }
        }


你不能触发这个事件,你只能处理它。在.NET中,除了从声明事件的类型的代码之外,您不能在任何类/结构中调用任何事件。这是一个针对事件的.NET万无一失的功能;在这方面,事件与常规委托实例不同。



因此,在这方面,问题根本没有意义。此外,您询问的事件的处理与任何事物中的列数完全无关。也许,你有一些真正的问题,但你没有描述任何问题。



-SA
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


这篇关于当表中有多个列时,如何在datagridview上触发click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆