DataGridview, disable Ctrl+Click - vb.net

I have a datagridview as such:
With DataGridView1
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.ReadOnly = True
.AllowUserToAddRows = True
.MultiSelect = False
.EditMode = DataGridViewEditMode.EditProgrammatically
End With
When I do Ctrl + Click on last row my datagridview lost wanted functionality.
Sometimes select a row sometimes deselect and those little black triangle always jumps one row to lower.
I think this may be built in functionality for adding new data to grid.
Since I need my own Ctrl + Click on last row is there any chance to turn off those built in functionality so last row can behave as any other?

I am not sure about what you mean with a special functionality when Ctrl+Clicking on a cell. But if what you want is a code being triggered when a cell is clicked and Ctrl is pressed, you can rely on the CellMouseClick event:
Private Sub DataGridView1_CellMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
If (Control.ModifierKeys = Keys.Control) Then
MessageBox.Show("CTRL & Click")
End If
End Sub

Related

Not allow selection of a certain column in a DataGridView control

I have this datagridview
I was wondering if there is a way to disable the abillity of the user to select cells of the first column (the one with an arrow), but i still need the user to be able to select all other cells,exept the ones on the first column.
If the selection is moved to the target column, using the DataGridView1.SelectionChanged event you can prevent the selection focus, setting the DataGridView1.CurrentCell to next column cell in the same row.
This works for selection events generated by both Mouse clicks and cursor movement.
Private blockedColumn As Integer = 0
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If DataGridView1.CurrentCell.ColumnIndex = blockedColumn Then
DataGridView1.CurrentCell =
DataGridView1(blockedColumn + 1, DataGridView1.CurrentCell.RowIndex)
End If
End Sub
You could set dataGridView1.Columns(0).Frozen = True anyway, for other uses. But it's not necessary.

refresh datagridview after cellformatting event

I'm programming in Visual Basic Net and I fill a Datagridview with a MySql table manually. I want to change font, style of a row if meet some conditions.
The Datagridview.CellFormatting event can do that, but after leave the event my datagridview don't refresh. Below the code:
Private Sub gridUsers_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles gridUsers.CellFormatting
With DirectCast(sender, DataGridView)
If .CurrentRow.Cells("eliminado").Value Is "Si" Then
.CurrentRow.DefaultCellStyle.BackColor = Color.DarkRed
.CurrentRow.DefaultCellStyle.ForeColor = Color.White
ElseIf .CurrentRow.Cells("condicion").Value Is "No" Then
.CurrentRow.DefaultCellStyle.ForeColor = Color.DarkRed
.CurrentRow.DefaultCellStyle.Font = New Font("Verdana", 8, FontStyle.Strikeout Or FontStyle.Bold)
End If
End With
End Sub
I don't know why, but whenever I click any row in the grid, or scroll down and up, the lines' colors change. How to refresh the style of the cells in the datagridview
Thanks for your help...

Hide a row in DataGridView

I am a new user to vb.net and need to hide a row when a user right clicks on a contextmenu and selects hide. I have googled this but have yet to find a way to do it.
At the moment, when a user clicks on an entry in the grid, the value is entered into a text box which is fine. What I need to do is hide the entry the user right clicked on and hide the selection. As I am new I am finding it hard going to code this as I have just finished my first course which entailed the basics. Any help would be appreciated or if you need anymore code, then please ask.
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
'Get the text of the item that was clicked on.
'Dim text As String = txtCustomerActive.Text
Try
'txtCustomerActive.Visible = False
pnlContextMenuStrip1.Visible = False
MessageBox.Show(txtCustomerActive.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
You could use Rows.Item() to hide specific DataGridViewRow, like:
If (UserDataGridView.Rows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
End If
I am assuming you are using FullRowSelect here.
If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:
If (UserDataGridView.SelectedRows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
Next
End If
To Unhide everything let's say from a Button Click you could have this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In UserDataGridView.Rows
If (row.Visible = False) Then
UserDataGridView.Rows.Item(row.Index).Visible = True
End If
Next
End Sub
As far as I know, you can not make a server-side handler for right mouse click (as you did for HideToolStripMenuItem_Click, which works as part of .NET postback mechanism).
However, I believe that such feature could be done with some client-side javascript progamming.
Hope this helps!

Using combobox from class

I have some questions on using of combobox.
1) I need to reference combobox from class like this:
If Me.ActiveControl.GetType Is GetType(ComboBox) And combodroppeddown = False) Then
something...
End If
From Here I need right from the AND to check if this combobox is dropped down but I don't know how.
2) My actual type of combobox is of "DropDownList" type.
Problem is that if I drop it down and type with up/down keys the value is changed according to selected row. If I then press ESC then last value stays as choosen what is not wanted.
Is here way to return original value from moment of dropping in case if I press ESC when list is dropped?
How to do that?
Here is closer look to my xCombo subclass to get help in second question...
Public Class xAutoCombo
Inherits ComboBox
Private entertext As String
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
If e.Control Then ' this dropped a list with keyboard
If e.KeyCode = Keys.Down Then
Me.DroppedDown = True
End If
End If
'' this should return value back if ESC was pressed
'' but don't work!
If Me.DroppedDown And e.KeyCode = Keys.Escape Then
Me.Text = entertext
End If
MyBase.OnKeyDown(e)
End Sub
Protected Overrides Sub OnDropDown(ByVal e As System.EventArgs)
entertext = Me.Text '' this remember text in moment of droping
MyBase.OnDropDown(e)
End Sub
EDIT:
Here I found one issue in functionality which I like to be solved.
When combo is dropped and I navigate through list by keyboard and then press with mouse to form (or outside of combo) it closes a list and set value which is last been selected.
Instead of that I would like that combo set his new value ONLY on click with mouse to list or with pressing Enter key with keyboard.
Examine the DroppedDown property, but it seemed like you have other things you wanted to do while dropped down.
Dim cbo As ComboBox
If Me.ActiveControl.GetType Is GetType(ComboBox) then
cbo=Ctype(Me.ActiveControl, ComboBox)
' make it easier to refernece
if cbo.DroppedDOwn then
....
End iF
End if
' Note:
Ctype(Me.ActiveControl, ComboBox).DroppedDown
' should work, but the above is easier to read and use since you apparently
' will have other things to do/override with it
Note also that I think one of the three combobox dropdown types does not use/support the DroppedDown property.
For the rest of your question, which I dont entirely follow, you could also store the last selected item and restore it in similar fashion. Overriding windows default behavior though, is rarely a good idea because you are creating something the user has never encountered before.
EDIT
To change Escape functionality from CLOSE DROPDOWN to ABORT CHOICE:
NOTE: I just used a std cbo, refs will need to be changed to MyBase, events to On...
Private selectedindex As Integer = -1
Private bEsc As Boolean = False
Private Sub cbo_Enter(....
' reset tracking vars...might not be needed
selectedindex = -1
bEsc = False
End Sub
Private Sub cbo_DropDown(...
' capture starting state
selectedindex = cbo.SelectedIndex
bEsc = False
End Sub
KeyDown:
If cbo.DroppedDown AndAlso e.KeyCode = Keys.Escape Then
bEsc = True
e.Handled = True
' this MUST be last!
cbo.DroppedDown = False
End If
Private Sub cbo_DropDownClosed(...
' rest cbo.selectedindex if escape was pressed
If bEsc Then
' note: SelectedIndexChanged event will fire and any code there will run
' may need to qualify with If Esc = False...
cbo.SelectedIndex = selectedindex
End If
End Sub

DataGridView only refreshing styles upon cell click

I have a DGV in VB.NET and VS2012. I am attempting to change the cell formatting of various cells dynamically. The following is my code:
Private Sub gridFinancial_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles gridFinancial.CellFormatting
Try
For Each row As chgltrDataSet.gridsourceRow In frmFinBatchChrg.ChgltrDataSet.gridsource.Rows
If gridFinancial.CurrentRow.Cells("CompBool").Value = True Then
Me.gridFinancial.CurrentRow.Cells(0).Style.BackColor = Color.Yellow
Me.gridFinancial.CurrentRow.Cells(1).Style.BackColor = Color.Yellow
Me.gridFinancial.CurrentRow.Cells(2).Style.BackColor = Color.Yellow
Me.gridFinancial.CurrentRow.Cells(3).Style.BackColor = Color.Yellow
Me.gridFinancial.CurrentRow.Cells(4).Style.BackColor = Color.Yellow
Me.gridFinancial.CurrentRow.Cells(5).Style.BackColor = Color.Yellow
Me.gridFinancial.CurrentRow.Cells(6).Style.BackColor = Color.Yellow
Me.gridFinancial.CurrentRow.Cells(7).Style.BackColor = Color.Yellow
Me.gridFinancial.CurrentRow.Cells(8).Style.BackColor = Color.Yellow
Me.gridFinancial.CurrentRow.Cells(0).ReadOnly = True
Me.gridFinancial.CurrentRow.Cells(1).ReadOnly = True
Me.gridFinancial.CurrentRow.Cells(2).ReadOnly = True
Me.gridFinancial.CurrentRow.Cells(3).ReadOnly = True
Me.gridFinancial.CurrentRow.Cells(4).ReadOnly = True
Me.gridFinancial.CurrentRow.Cells(5).ReadOnly = True
Me.gridFinancial.CurrentRow.Cells(6).ReadOnly = True
Me.gridFinancial.Update()
Me.gridFinancial.Refresh()
End if
Catch ex As Exception
End Try
End Sub
I have read this: http://msdn.microsoft.com/en-us/library/1yef90x0.aspx and maybe I'm missing something, but right now, with that code applied, my DataGridView will only reflect that code if I click one of the affected cells after the DataGridView has been painted. In other words, after the DataGridView has loaded, the cells will only be yellow after I click them (then all of the cells within that row that are supposed to be yellow, appear yellow). Why is this? I'm not sure what I'm doing wrong.
And as a side question, this cell formatting event fires at least 40-50 times before my DGV has even been drawn, and it's only a 6 row DataSource. Isn't there a better event trigger for this? I'm sure my code could be better, but that just seems highly inefficient.
The readonly properties in the above code work fine, so I know the event is triggering correctly.
Instead of setting the cell style like you are you can access the style through the DataGridViewCellFormattingEventArgs.
So something like this:
e.CellStyle.BackColor = Color.Yellow
Another thing to consider is that it is often better to attach your CellFormatting handler from within the DataBindingComplete event.
Private Sub DataGridView1_DataBindingComplete(sender As System.Object, e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
AddHandler DataGridView1.CellFormatting, AddressOf Me.DataGridView1_CellFormatting
End Sub
This addresses some odd behaviour when the visual portion of the DataGridView is rendered by making sure that everything is done before your later events are attached.
This tip with the DataBindingComplete event should at least reduce the needless calls to CellFormatting - cell formatting fires every time a cell is updated. Once you are all up and running this is only once a user leaves a cell.