【问题标题】:How to disable HeaderText Click in c#?如何在 c# 中禁用 HeaderText Click?
【发布时间】:2020-02-20 07:22:51
【问题描述】:

我在 datagridview 中更改了我的标题文本。 datagridview 中的数据工作正常,但是当我单击 datagridview 中的标题文本时,错误显示“索引超出范围。必须是非负数且小于集合的大小。参数名称:索引'。如何在 c# 中禁用 HeaderText 单击?

注意:我不想使用标题对数据进行排序。我只是想禁用它。

提前致谢!

【问题讨论】:

标签: c#


【解决方案1】:

如果点击标题行有问题,可以使用以下代码检查问题

如果单击发生在列标题中,则 RowIndex 将为 -1。这可能是一个快速的解决方案:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0)
            {
                MessageBox.Show("Clicked cell");
            }

            else if (e.RowIndex < 0)
            {
                MessageBox.Show(" header Clicked'");

            }
        }

如果只有行索引> 0就做事

如果问题出在排序上,请点击以下链接。

Disable sorting when clicking DataGridView column header

【讨论】:

  • 现在工作正常。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 2015-09-01
  • 2014-05-15
  • 2012-09-09
  • 1970-01-01
  • 2014-07-07
相关资源
最近更新 更多