C#gridview 多选
时间: 2023-12-05 08:40:57 浏览: 153
以下是C#中使用GridView实现多选的示例代码:
```csharp
private void gridView1_MouseDown(object sender, MouseEventArgs e)
{
GridView view = sender as GridView;
GridHitInfo info = view.CalcHitInfo(new Point(e.X, e.Y));
if (info.InRow && info.HitTest != GridHitTest.RowIndicator)
{
view.FocusedRowHandle = info.RowHandle;
if (Control.ModifierKeys == Keys.Shift)
{
int startRow = Math.Min(info.RowHandle, lastRowHandle);
int endRow = Math.Max(info.RowHandle, lastRowHandle);
SelectRows(startRow, endRow);
}
else if (Control.ModifierKeys == Keys.Control)
{
if (view.IsRowSelected(info.RowHandle))
{
view.UnselectRow(info.RowHandle);
}
else
{
view.SelectRow(info.RowHandle);
}
lastRowHandle = info.RowHandle;
}
else
{
SelectRows(info.RowHandle, info.RowHandle);
lastRowHandle = info.RowHandle;
}
}
}
private void SelectRows(int startRow, int endRow)
{
for (int i = startRow; i <= endRow; i++)
{
gridView1.SelectRow(i);
}
}
```
以上代码实现了在GridView中使用鼠标进行多选的功能,支持Shift键和Ctrl键进行多选操作。
阅读全文