One of my mates has asked my about how to enable a datagridview multiselection without pressing CTRL key (for a touch screen). I haven't tested it throughly, but here comes a (C#) possible solution:
I´ve tested it only with a Datagridview configured for full row selection.
Basically I hold a private collection of the rows I want to select and every time a row is pressed, it´s selected or removed accordingly
The empty override method avoids selection refresh. Try to remove it and see...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
namespace MyDataGridView
{
public partial class MyDataGridV : DataGridView
{
private Hashtable mySelectedRows = new Hashtable();
public MyDataGridV()
{
}
protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
{
}
protected override void OnCellClick(DataGridViewCellEventArgs e)
{
if (mySelectedRows.ContainsKey(e.RowIndex))
{
mySelectedRows.Remove(e.RowIndex);
}
else
{
mySelectedRows.Add(e.RowIndex, this.Rows[e.RowIndex]);
}
for (int i = 0; i < this.Rows.Count; i++)
{
if (mySelectedRows.Contains(i))
{
this.SetSelectedRowCore(i, true);
}
else
{
this.SetSelectedRowCore(i, false);
}
}
Debug.Assert(this.SelectedRows.Count == mySelectedRows.Count, "Selected rows number don´t match");
base.OnCellClick(e);
}
}
}
Subscribe to:
Post Comments (Atom)
2 comments:
Create a form1 with visual basic
Insert a button called btnrRegistar
Insert a button called btnDeselectAll
Copy and paste this code to the form1.
Only the middle columns can be clicked.
One cell in the same row deselect the other
Public Class Form1
Dim WithEvents dg1 As New MyDataGridView(Me)
Private Class MyDataGridView
Inherits System.Windows.Forms.DataGridView
Private myHashTable As New Collections.Hashtable
Dim s As String
Dim f As Form1
Public Sub DeselectAll()
myHashTable.Clear()
End Sub
Public Sub New(ByVal f1 As Form1)
f = f1
Me.AllowUserToAddRows = False
Me.AllowUserToDeleteRows = False
Me.AllowUserToResizeColumns = False
Me.AllowUserToOrderColumns = False
Me.ReadOnly = True
End Sub
Protected Overrides Sub OnCellMouseDown(ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
If (e.RowIndex >= 0) AndAlso _
(e.RowIndex < Me.Rows.Count) AndAlso _
(e.ColumnIndex >= 1) AndAlso _
(e.ColumnIndex <= 2) Then
'A cell was clicked
s = e.ColumnIndex.ToString & "/" & e.RowIndex.ToString
'If the cell is on the hashtable, remove it
If myHashTable.ContainsKey(s) Then
myHashTable.Remove(s)
Me.Item(e.ColumnIndex, e.RowIndex).Selected = False
Else ' otherwise, insert ir on the hashtable
'if some cell was selected in the same row, remove it
Select Case e.ColumnIndex
Case 1 'clicked on the column 1, check if the cell in the column 2
'had been previously selected
If myHashTable.ContainsKey("2/" & e.RowIndex.ToString) Then
myHashTable.Remove("2/" & e.RowIndex.ToString)
Me.Item(2, e.RowIndex).Selected = False
End If
Case 2 'clicked on the column 2, check if the cell in the column 1
'had been previously selected
If myHashTable.ContainsKey("1/" & e.RowIndex.ToString) Then
myHashTable.Remove("1/" & e.RowIndex.ToString)
Me.Item(1, e.RowIndex).Selected = False
End If
End Select
'add to the hash table
myHashTable.Add(s, Me.Item(e.ColumnIndex, e.RowIndex))
End If
'select all cells clicked
For Each SelectedCell As System.Windows.Forms.DataGridViewCell In myHashTable.Values
SelectedCell.Selected = True
Next
f.btnRegistar.Enabled = myHashTable.Count > 0
f.Label1.Text = myHashTable.Count.ToString
'MyBase.OnCellMouseDown(e) removed to prevent being called
End If
End Sub
End Class 'MyDataGridView
Private Sub btnDeselectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeselectAll.Click
dg1.ClearSelection()
dg1.DeselectAll()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dg1.Columns.Add("A", "A")
dg1.Columns.Add("B", "B")
dg1.Columns.Add("C", "C")
dg1.Columns.Add("D", "D")
dg1.Rows.Add(15)
dg1.Location = New Point(10, 10)
dg1.Size = New Size(500, 500)
Controls.Add(dg1)
dg1.ClearSelection()
btnRegistar.Enabled = False
End Sub
End Class
[url=http://www.23planet.com]casinos online[/url], also known as agreed casinos or Internet casinos, are online versions of traditional ("buddy and mortar") casinos. Online casinos definite someone experience gamblers to sham and wager on casino games with the grant-money the Internet.
Online casinos superficially bring up up owing present odds and payback percentages that are comparable to land-based casinos. Some online casinos altercate on higher payback percentages with a set before one's make in prominence motor buggy games, and some impel known payout behalf audits on their websites. Assuming that the online casino is using an correctly programmed unspecific convoy generator, programme games like blackjack be blessed an established congress edge. The payout subdivision as a replacement representing these games are established bad non-standard paraphernalia to the rules of the game.
Uncountable online casinos apply field or be established their software from companies like Microgaming, Realtime Gaming, Playtech, Prevailing Campaign Technology and CryptoLogic Inc.
Post a Comment