创建一个gridview事件,它选择相同列中的一些元素
本文关键字:元素 选择 一个 gridview 创建 事件 | 更新日期: 2023-09-27 18:04:22
我有一个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
}
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个事件。CellMouseDown
, CellMouseUp
, MouseLeave
.
在CellMouseDown
中,我们获得初始选定Cell的信息并将其保存在rowindex和columnindex中。buttonPressed被设置为true(这在后面可能会有帮助)。
如果在鼠标指针指向单元格时释放鼠标按钮,则触发事件CellMouseUp
。我们首先检查哪个细胞更靠近DGV的顶部(开始的细胞或当前的细胞)。然后每个单元格都被取消选择。之后,我们检查我们选择的单元格的数量(存储在amountOfRows
中)。然后我们再次选择"正确的"单元格。
事件MouseLeave
也可能很重要。否则,用户可以通过开始选择一个单元格并将鼠标拖出DGV来"滥用"您的功能。如果没有这个功能,他将能够选择多个列。
如果你还有什么问题,请尽管问。