Multiple selection in datagridview - vb.net

I work on a new project that need a multiple row selection/deselection by the user in a datagridview with only a tap on a touch screen.
The form should look like this:
For exemple, if the user want to delete row 2 and 5, he only need to tap once on each line to select/deselect them. After the selection is done, he tap on "Delete Row" button.
I've already try to play with the CellClick event without success!!
Can someone have a clue how can I handle this problem?

After setting MultiSelect property to True and SelectionMode to FullRowSelect you can use a List to store which row of your DataGridView is selected.
On CellClick you can add/remove rows from your List, on RowPostPaint you can select a row if it's included in the List and on RowsRemoved you have to clear the List.
Private intSelectedRows As New List(Of Integer)
Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
With CType(sender, DataGridView)
Dim intRow As Integer = .CurrentRow.Index
If Not Me.intSelectedRows.Contains(intRow) Then
Me.intSelectedRows.Add(intRow)
Else
.CurrentRow.Selected = False
Me.intSelectedRows.Remove(intRow)
End If
End With
End Sub
Private Sub DataGridView1_RowPostPaint(sender As Object, e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
If Me.intSelectedRows.Contains(e.RowIndex) Then
CType(sender, DataGridView).Rows(e.RowIndex).Selected = True
End If
End Sub
Private Sub DataGridView1_RowsRemoved(sender As Object, e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
Me.intSelectedRows.Clear()
End Sub
If you want to clear selection you can use this code:
Private Sub btnClearSelectedRows_Click(sender As System.Object, e As System.EventArgs) Handles btnClearSelectedRows.Click
For Each intSelectedRow As Integer In Me.intSelectedRows
Me.DataGridView1.Rows(intSelectedRow).Selected = False
Next intSelectedRow
Me.intSelectedRows.Clear()
End Sub

Related

Moving average method VB

i'd try to make moving average in vb
i want to check the cells and set the value to text box
but the result is all the text box has the same value
how to make my first check value (penjualan/bulan) is inputed into first text box and the second check (penjualan/bulan) to second text box.
here is my code
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = 5 Then
tb1.Text = DataGridView1.CurrentRow.Cells(3).Value
tb2.Text = DataGridView1.CurrentRow.Cells(3).Value
tb3.Text = DataGridView1.CurrentRow.Cells(3).Value
End If
End Sub
thanks.
You set EVERY time the cellClicked-event is raised all 3 Textboxes to the same value, CurrentRow.Cells(3).Value.
Another problem is that your code will set the text in the Textboxes always. It dont check if the Checkbox is checked or not. it just updated every time you click in any cell in this column, the text in the 3 boxes to the value of your currently selected row.
Here you have a solution. Its not perfect but should work, although you should try to understand and optimize it.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeDgv()
End Sub
Private Sub InitializeDgv ()
Dim row as String() = New String(){"2016",240}
DataGridView1.Rows.Add(row)
row = New String(){"2017",223}
DataGridView1.Rows.Add(row)
row = New String(){"2015",54}
DataGridView1.Rows.Add(row)
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
if e.ColumnIndex=2
Dim checkedRows=(From dgv as DataGridViewRow in DataGridView1.Rows where dgv.Cells(2).Value=True select dgv).ToList()
Dim controlsList As new List(of TextBox)
controlsList.Add(TextBox1)
controlsList.Add(TextBox2)
controlsList.Add(TextBox3)
for Each item in controlsList
item.Text=String.Empty
Next
for i=0 to checkedRows.Count-1
controlsList(i).Text=checkedRows.Item(i).Cells(0).Value
Next
End If
End Sub
End Class

vb.net how to use shift for selecting multiple checkbox

How to change state of multiple CheckBoxes in DataGridView by pressing Shift key and select the checkbox.
Is it possible to do it in vb.net?
Thanks in advance.
Here's an example of how that could be accomplished. For this example, I just created a datagridview with two columns, one text and one checkbox, and added 5 rows to it. The key is the datagridview_click event. When that event gets called, it will check if the shift key was being held down with the click. If it was, it will uncheck every datagridviewcheckboxcell in your selection:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim c1 As New DataGridViewTextBoxColumn
DataGridView1.Columns.Add(c1)
Dim c2 As New DataGridViewCheckBoxColumn
DataGridView1.Columns.Add(c2)
For i As Integer = 1 To 5
DataGridView1.Rows.Add("test" & i.ToString, True)
Next i
End Sub
Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
If My.Computer.Keyboard.ShiftKeyDown Then
For Each cell As DataGridViewCell In DataGridView1.SelectedCells
If TypeOf cell Is DataGridViewCheckBoxCell Then cell.Value = False
Next
DataGridView1.RefreshEdit()
End If
End Sub

Add cell click event handler to datagridview?

I have three columns, first column is textbox, second column is checkbox, third column is textbox. I want to add a click event to the third column where if a user click on that cell, it will automatically checkmark and uncheckmark the second checkbox column of that row. I tried this but it's not working.
AddHandler datagridview1.MouseClick, AddressOf form1.datagridview1_MouseClick
just need to switch your Handle type on the subroutine to "Handles DataGridView1.CellClick". Example:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim tempView = DirectCast(sender, DataGridView)
For Each cell As DataGridViewTextBoxCell In tempView.SelectedCells
If cell.ColumnIndex = 1 Then
Dim tempCheckBoxCell As DataGridViewCheckBoxCell = tempView("column1", cell.RowIndex)
tempCheckBoxCell.Value = True
End If
Next
End Sub
Also, quick note - you will need to adjust the cell type found in the for each loop to whatever type of cell you are using; in the example I had column2 set to a simple textbox type cell.
Look at the CellClick event. https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick(v=vs.110).aspx
Something like:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With DataGridView1
.Rows.Add({"John Smith", 1})
.Rows.Add({"Jane Doe", 0})
End With
AddHandler DataGridView1.CellClick, AddressOf DataGridView1_CellClick
End Sub
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs)
If e.RowIndex < 0 Then Exit Sub
Dim dgv As DataGridView = CType(sender, DataGridView)
If Not TypeOf dgv.Rows(e.RowIndex).Cells(e.ColumnIndex) Is DataGridViewCheckBoxCell Then Exit Sub
Dim cell As DataGridViewCheckBoxCell = CType(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewCheckBoxCell)
cell.Value = Not CBool(cell.Value)
dgv.EndEdit()
End Sub
End Class

VB.NET - Datagridview: Get index of clicked column

In VB.NET's DataGridView, how can i get the index of the column that was clicked on, instead of the one that that has a selected cell.
I wish to provide the user with the option to right click the column and hide it, via a context menu. This code gives me the index of the column that has a selected cell:
Private Sub dataGridView1_ColumnHeaderMouseClick(sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles dataGridView1.ColumnHeaderMouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
currSelectedColIdx = e.ColumnIndex
ContextMenuStrip1.Show()
End If
End Sub
Edit: The problem happens when i bind the contextmenu to the datagridview via the properties window. If i unbind it, the code works correctly.
You can use CellContextMenuStripNeeded event of DataGridView.
Private Sub DataGridView1_CellContextMenuStripNeeded(sender As Object, e As DataGridViewCellContextMenuStripNeededEventArgs) Handles DataGridView1.CellContextMenuStripNeeded
If e.RowIndex = -1 Then
e.ContextMenuStrip = ContextMenuStrip1
'e.ColumnIndex is the column than you right clicked on it.
End If
End Sub
you can get index of column with : e.ColumnIndex.
Your have probably probleme with declaration of your variable "currSelectedColIdx " :
Please try this code :
Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim currSelectedColIdx = e.ColumnIndex
ContextMenuStrip1.Show(Cursor.Position)
End If
End Sub

Delete row from DataGridView via button on the row

How can I delete a DataGridView row via a button on the row?
screenshot: http://i.imgur.com/8342MKT.png
When the user clicks the '삭제' button on a row, I want to delete that row.
I tried this:
For Each row As DataGridViewRow In DataGridView1.SelectedRows
DataGridView1.Rows.Remove(row)
Next
But although this worked for a button outside the DataGridView, it doesn't work with a button in the DataGridView.
How can I make this work with the button in the DataGridView?
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To 5
DataGridView1.Rows.Add(i.ToString, i.ToString, i.ToString, "삭제")
Next
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.ColumnIndex = 3 Then
DataGridView1.Rows.RemoveAt(e.RowIndex)
End If
End Sub
End Class