vb.net how to use shift for selecting multiple checkbox - vb.net

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

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

Multiple selection in datagridview

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

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

Datagridview_cellvaluechanged only updates on second try

DirtyStateChanged lets me commit immediately when I click the checkbox
Private Sub dataGridView4_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGridView4.CurrentCellDirtyStateChanged
If DataGridView4.IsCurrentCellDirty Then
DataGridView4.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
Private Sub DataGridView4_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView4.CellValueChanged
For c As Integer = 2 To DataGridView4.ColumnCount - 1
If DataGridView4.Rows(e.RowIndex).Cells(c).Value = True Then
'Disable this persons' row on datagridview5
Dim aName As String = DataGridView4.Rows(e.RowIndex).Cells(0).Value
For Each dRow As DataGridViewRow In DataGridView6.Rows
If dRow.Cells(0).FormattedValue = aName Then
dRow.ReadOnly = True
For Each cell As DataGridViewCell In dRow.Cells
dRow.Cells(cell.ColumnIndex).Style.BackColor = Color.LightGray
Next
'DataGridView6.Update()
End If
Next
End If
Next
End Sub
When I click a checkbox on datagridview3, it should update the row on datagridview5 with a new backcolor and readOnly.
What seems to happen is it does not seem to work the first time clicking the checkbox. It does work perfectly on the second try though.
How can I make it validate on the first try?

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