首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建选择相同列的某些元素的网格视图事件

创建选择相同列的某些元素的网格视图事件
EN

Stack Overflow用户
提问于 2013-03-18 20:08:01
回答 1查看 435关注 0票数 0

我有一个网格视图,里面有一些来自数据库的单元格。用户应该一次只能选择1列的行。示例:

代码语言:javascript
运行
复制
   0   1   2  
0  a   b   c      
1  d   e   f     
2  g   h   i

如果用户将鼠标从0/0 =a拖动到1/1 =e(同时按下左键),则只应选择单元格a和d。(不允许多列选择)

如果有人能帮上忙

谢谢

这就是我一开始得到的:

代码语言:javascript
运行
复制
private void myDataGridView1_MouseDown(object sender, MouseEventArgs e) {

            var hti = dataGridView1.HitTest(e.X, e.Y);
            coord[0] = hti.RowIndex;
            coord[1] = hti.ColumnIndex;
}

private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
        {
            var hti = dataGridView1.HitTest(e.X, e.Y);
            coord[2] = hti.RowIndex;
            coord[3] = hti.ColumnIndex;

            // Some actions 
}

此示例仍选择多个列。

EN

回答 1

Stack Overflow用户

发布于 2013-04-10 22:40:52

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace gridview
{
    public partial class Form1 : Form
    {
        bool buttonIsPressed = false;
        int rowIndex, columnIndex;

        public Form1()
        {
            InitializeComponent();
            completeDataGridview();
        }

        //this function only creates a filled DataGridView
        private void completeDataGridview()
        {
            dataGridView1.RowCount = 3;
            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    dataGridView1.Rows[x].Cells[y].Value = ((char)(97 + y + 3 * x)).ToString();
                }
            }

            // this part is stolen from http://stackoverflow.com/questions/7412098/fit-datagridview-size-to-rows-and-columnss-total-size
            int height = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                height += row.Height;
            }
            height += dataGridView1.ColumnHeadersHeight;

            int width = 0;
            foreach (DataGridViewColumn col in dataGridView1.Columns)
            {
                width += col.Width;
            }
            width += dataGridView1.RowHeadersWidth;

            dataGridView1.ClientSize = new Size(width + 2, height);

        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            buttonIsPressed = true;
            rowIndex = e.RowIndex;
            columnIndex = e.ColumnIndex;            
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (buttonIsPressed)
            {
                int startingRow = 0;
                if (rowIndex > e.RowIndex)
                    startingRow = e.RowIndex;
                else
                    startingRow = rowIndex;

                //at first we have to deselect every cell
                deselectEveryCell();

                //then reselect the old ones
                int amountOfRows = Math.Abs(rowIndex - e.RowIndex) + 1;
                for (int x = 0; x < amountOfRows; x++)
                {
                    dataGridView1.Rows[startingRow + x].Cells[columnIndex].Selected = true;
                }
            }
            buttonIsPressed = false;
        }

        private void deselectEveryCell()
        {
            for (int x = 0; x < dataGridView1.RowCount; x++)
            {
                for (int y = 0; y < dataGridView1.ColumnCount; y++)
                {
                    dataGridView1.Rows[x].Cells[y].Selected = false;
                }
            }
        }

        private void dataGridView1_MouseLeave(object sender, EventArgs e)
        {
            if (buttonIsPressed)
                deselectEveryCell();
        }
    }
}

这是您需要的代码。函数completeDataGridview()并不重要。

你需要3个事件。CellMouseDownCellMouseUpMouseLeave

CellMouseDown中,我们获取初始选定单元格的信息,并将其保存在rowindex和columnindex中。并将buttonPressed设置为true (这在以后可能会有所帮助)。

如果在鼠标指针指向单元格时松开鼠标按钮,则会触发事件CellMouseUp。我们首先检查哪个单元格更靠近DGV的顶部(开始的单元格还是当前的单元格)。然后取消选择每个单元格。然后,我们检查我们选择的单元格的数量(存储在amountOfRows中)。然后我们再次选择“正确”的单元格。

事件MouseLeave也可能很重要。否则,用户可以通过开始选择一个单元格并将鼠标拖出DGV来“滥用”您的功能。如果没有该函数,他将能够选择多个列。

如果您还有任何问题,请不要犹豫地问。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15476494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档