如何获取DataGrid当前单元格的行和列位置 [英] How to get datagrid current cell row and column position

查看:497
本文介绍了如何获取DataGrid当前单元格的行和列位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个datagridview,我不希望第一列可以使用文本框进行编辑.因此,我需要知道当前单元格的列和行的位置.

I have create a datagridview, and I don''t want the first column to be editable with textboxes. So, I need to know the location of the current cell column and row.

And do I get the possition?

推荐答案

如果整个第一列都不是可编辑的,则可以将列的ReadOnly属性设置为True.设置网格.可能在LoadEvent中:

dgvGrid.Columns("columnName").ReadOnly = True
If the entire first column should not be editable, you can just set the column''s ReadOnly property to True when you are setting up the grid. Probably in the LoadEvent:

dgvGrid.Columns("columnName").ReadOnly = True




使用datagridview的内置事件.为属性浏览器中的datagridview添加并设置一个contextmenustrip.当用户右键单击它时,为网格添加所需的菜单项.

最后在以下事件中编写代码:

Hi,

Use the inbuilt event of the datagridview. Add and set a contextmenustrip for the datagridview from property explorer. Add menuitems you want for the gridrow when user rightclicks it.

Finally write your code in the following event:

private void dgvProducts_RowContextMenuStripNeeded(object sender, DataGridViewRowContextMenuStripNeededEventArgs e)
        {
            int indexOfClickedRow = e.RowIndex;
            dgvProducts.Rows[indexOfClickedRow].Selected = true;
           
            if(indexOfClickedRow==1) //or 0
            {
                  //Do Something
            }
            else
            {
                  //Do Something
            }
        }


这篇关于如何获取DataGrid当前单元格的行和列位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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