【问题标题】:Use hand cursor only on a certain column of DataGridView仅在 DataGridView 的某一列上使用手形光标
【发布时间】:2017-10-05 18:46:19
【问题描述】:

我正在用 C# 编写一个表单应用程序,并且我有一个 DataGridView,它显示来自 SQL 服务器的数据。 这些数据包含一个名为Remove 的列,并且该列的所有行都包含字符串Remove。现在我想通过更改背景颜色和使用手形光标使该列的所有单元格看起来像一个按钮。

我的问题是,我不能只在此列上使用手形光标。我想要的是,当鼠标悬停在此 Remove 列的任何行上时,将鼠标指针更改为手形光标,而不是其他任何地方。

for(int i=0; i<myDataGridView.RowCount; i++){
    myDataGridView.Cursor = Cursors.Hand;
}

没有做我想做的事,因为鼠标指针在 DataGridView 上的任何地方都变成了手形光标,而不仅仅是在 Remove 列上。 我尝试了类似的东西

for(int i=0; i<myDataGridView.RowCount; i++){
    myDataGridView.Columns["Remove"].Cursor = Cursors.Hand;
}

但这会报错:

System.Windows.Forms.DataGridViewColumn 不包含“光标”的定义。

有什么好的方法可以实现吗?谢谢。

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

尝试点击 DataGridView 的 OnCellMouseEnter 事件。事件触发后,您可以确定单元格所在的列并根据需要更改光标。

【讨论】:

    【解决方案2】:

    使用此代码,它对我有用

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
            {
                string colname = dataGridView1.Columns[e.ColumnIndex].Name;
               if(colname!="btnEdit" && colname!= "btnDelete")
                {
                    dataGridView1.Cursor = Cursors.Default;
                }
                else
                {
                    dataGridView1.Cursor = Cursors.Hand;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2015-01-02
      • 2012-03-16
      • 1970-01-01
      • 2011-12-22
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      相关资源
      最近更新 更多