创建一个gridview事件,选择相同列的某些元素

时间:2013-03-18 12:08:01

标签: c# events datagridview

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

   0   1   2  
0  a   b   c      
1  d   e   f     
2  g   h   i

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

如果有人可以提供帮助,

谢谢

这就是我一开始就得到的:

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 
}

此示例仍会选择多个列。

1 个答案:

答案 0 :(得分:0)

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来“滥用”您的功能。如果没有该功能,他将能够选择多个列。

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