C# 创建一个gridview事件,选择相同列的某些元素
我有一个gridview和数据库中的一些单元格。用户一次只能选择一列中的行。例如:C# 创建一个gridview事件,选择相同列的某些元素,c#,events,datagridview,C#,Events,Datagridview,我有一个gridview和数据库中的一些单元格。用户一次只能选择一列中的行。例如: 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, MouseEventAr
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();
}
}
}
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间网格视图
{
公共部分类Form1:Form
{
bool buttonIsPressed=false;
int行索引,columnIndex;
公共表格1()
{
初始化组件();
completeDataGridview();
}
//此函数仅创建填充的DataGridView
私有void completeDataGridview()
{
dataGridView1.RowCount=3;
对于(int x=0;x<3;x++)
{
对于(int y=0;y<3;y++)
{
dataGridView1.Rows[x].Cells[y].Value=((char)(97+y+3*x)).ToString();
}
}
//这部分是从我的电脑里偷来的http://stackoverflow.com/questions/7412098/fit-datagridview-size-to-rows-and-columnss-total-size
整数高度=0;
foreach(dataGridView1.Rows中的DataGridViewRow行)
{
高度+=行高;
}
高度+=dataGridView1.ColumnHeadershight;
整数宽度=0;
foreach(dataGridView1.Columns中的DataGridViewColumn列)
{
宽度+=列宽度;
}
宽度+=dataGridView1.RowHeadersWidth;
dataGridView1.ClientSize=新尺寸(宽度+2,高度);
}
私有void dataGridView1_CellMouseDown(对象发送方,DataGridViewCellMouseEventArgs e)
{
按钮按下=真;
rowIndex=e.rowIndex;
columnIndex=e.columnIndex;
}
私有void dataGridView1_CellMouseUp(对象发送方,DataGridViewCellMouseEventArgs e)
{
如果(按下按钮)
{
int startingRow=0;
如果(rowIndex>e.rowIndex)
startingRow=e.RowIndex;
其他的
startingRow=行索引;
//首先,我们必须取消选择每个单元格
取消Everycell();
//然后重新选择旧的
int amountOfRows=Math.Abs(rowIndex-e.rowIndex)+1;
对于(int x=0;x
这是您需要的代码。
函数completeDataGridview()
并不重要
你需要3个项目CellMouseDown
,CellMouseUp
和MouseLeave
在CellMouseDown
中,我们获取初始选定单元格的信息,并将其保存在rowindex和columnindex中。buttonPressed设置为true(这在以后可能会有所帮助)
如果鼠标指针指向单元格时松开鼠标按钮,则会触发事件CellMouseUp
。我们首先检查哪个单元更靠近DGV的顶部(起始单元还是当前单元)。
然后取消选择每个单元格。
之后,我们检查我们选择的单元格数量(存储在amountOfRows
中)。
然后我们再次选择“正确”的单元格
事件MouseLeave
也可能很重要。否则,用户可以通过选择一个单元格并将鼠标拖出DGV来“滥用”ur功能。如果没有该功能,他将能够选择多个列
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();
}
}
}
如果您还有任何问题,请毫不犹豫地提问。确定您想选择同一行中的单元格。但如何确定应选择哪列中的哪一行呢。你用什么活动?好的,你的英语对我来说有点晦涩,所以你到底想要什么。即使用户选择了多个列,也只选择一个列的元素。是的,对不起,我的不好,我有很多问题要说,但是理解你没关系。但是是的,就是这样。如果用户开始在列“A”中选择,他不能选择多列,只能选择列“A”中的行。好的,我现在知道了。我会纠正你的问题,明天给你答案。因为我没时间了。但这不应该是个问题;)这个问题仍然没有答案。