将datagridview的当前单元格选择更改为行选择 [英] changing the current cell selection of datagridview to row select

查看:77
本文介绍了将datagridview的当前单元格选择更改为行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体,我们有一个datagridview。这是属性选择的属性。我有一个contextmenustrip,其中有一个菜单名为select all当select all被点击它应该将所选单元格的datagridview **的属性改为完整行选择,选择应该在同一行上我点击了。问题是,当我单击一个单元格时,默认属性是单元格选择,当我单击选择所有上下文menustrip时,未选中所选单元格,我必须重新选择我想要的那个行,当表单打开和单击时在特定的单元格上,当我点击选择所有的contextmenustrip点击然后应该选择我之前点击过单元格的同一行

这是我的代码



  private   void  selectAllToolStripMenuItem_Click( object  sender,EventArgs e)
{

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}

解决方案

没有自动的方式去做 - 事实上除了蛮力和无知之外几乎没有办法! :笑:

这是一个你想要的假人:如果你点击右键,它设置行选择,中间按钮设置单元格选择回来。

< pre lang =c#> private void dgvNames_MouseClick( object sender,MouseEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv!= null
{

if (e.Button == MouseButtons.Right)
{
DataGridViewSelectedCellCollection cells = dgv.SelectedCells;
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
foreach (DataGridViewCell cell in cells)
{
DataGridViewRow row = dgv.Rows [cell.RowIndex];
row.Selected = true ;
}
}
其他 如果(e.Button == MouseButtons.Middle)
{
DataGridViewSelectedRowCollection rows = dgv.SelectedRows;
dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
foreach (DataGridViewRow row in 行)
{
DataGridViewCell cell = row.Cells [ 0 ];
cell.Selected = true ;
}
}
}
}


i have a windows form in which we have a datagridview. the property of that is cell select. and i have a contextmenustrip in which there is a menu named select all when select all is clicked it should change the property of datagridview **of the selected cell** to the full row select and the selection should be on the same row which i have clicked. the problem is when i click on a cell the default property is cell select and when i click on select all of the context menustrip the selected cell is not selected and i have to reselect that row i want that when the form opens and when i click on particular cell and when i click on select all of the contextmenustrip is clicked then the same row should be selected on which i have clicked the cell previously
this is my code

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
   {

       dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
   }

解决方案

There is no automatic way to do it - indeed there is little but the Brute Force and Ignorance approach either! :laugh:
This is a dummy which does what you want: If you click the Right button, it sets Row select, the middle button sets cell select back.

private void dgvNames_MouseClick(object sender, MouseEventArgs e)
    {
    DataGridView dgv = sender as DataGridView;
    if (dgv != null)
        {

        if (e.Button == MouseButtons.Right)
            {
            DataGridViewSelectedCellCollection cells = dgv.SelectedCells;
            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            foreach (DataGridViewCell cell in cells)
                {
                DataGridViewRow row = dgv.Rows[cell.RowIndex];
                row.Selected = true;
                }
            }
        else if (e.Button == MouseButtons.Middle)
            {
            DataGridViewSelectedRowCollection rows = dgv.SelectedRows;
            dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
            foreach (DataGridViewRow row in rows)
                {
                DataGridViewCell cell = row.Cells[0];
                cell.Selected = true;
                }
            }
        }
    }


这篇关于将datagridview的当前单元格选择更改为行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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