datagridview单元格单击事件 [英] datagridview cell click event

查看:326
本文介绍了datagridview单元格单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在datagrid视图中有一个单元格单击事件,以在消息框中显示单击的单元格中的数据。我将其设置为仅对特定列有效,并且仅当单元格中有数据时才可用

I have an event for a cell click in a datagrid view to display the data in the clicked cell in a message box. I have it set to where it only works for a certain column and only if there is data in the cell

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.CurrentCell.ColumnIndex.Equals(3))
        if (dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null)
            MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}

但是,每当我单击任何列标题时,都会显示一个空白消息框。我不知道为什么,有任何提示吗?

however, whenever i click any of the column headers, a blank messagebox shows up. I cant figure out why, any tips?

推荐答案

您还需要检查单击的单元格不是列标题单元格。像这样:

You will also need to check the cell clicked is not the column header cell. Like this:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.CurrentCell.ColumnIndex.Equals(3) && e.RowIndex != -1){
        if (dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null)
            MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());   
}

这篇关于datagridview单元格单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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