datagridview editingcontrolshowing checkboxcell - vb.net

I want to change the backcolor of the edited cell when I have a checkboxcolumn in my datagridview.
For textboxcolumns the backcolor is easy to set but for some reason I don't catch this event on the checkboxcolumn when it goes into edit mode.
Is there something different I need to do to handle this scenario? I am guessing comboboxcolumns are similarly different?
Based on thetimmer's suggested answer I added the following code to the cellbeginedit event of my datagridview but it has no effect on the coloring.
If e.ColumnIndex = datagridview1.Columns("checkboxcolumn").Index Then
Dim c As DataGridViewCheckBoxCell = datagridview1.CurrentCell
c.Style.BackColor = Color.Red
End If

This works once you've left the cell
Private Sub DataGridView1_CellBeginEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
Me.DataGridView1.CurrentCell.Style.BackColor = Color.Yellow
End Sub

Related

DataGridView + TextBox How to colorate more and different values

I have a problem, with DataGridView's CellFormatting. The cells are colored by the search result from a TextBox. When I search for 2 numbers together, they are no longer colored. What should I do?
I state that I am using CONCAT_WS to load the table in DataGridView. What can I do?
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
Try
If e.ColumnIndex = 3 And e.Value IsNot Nothing Or e.ColumnIndex = 4 And e.Value IsNot Nothing Or e.ColumnIndex = 5 And e.Value IsNot Nothing Or e.ColumnIndex = 6 And e.Value IsNot Nothing Or e.ColumnIndex = 7 And e.Value IsNot Nothing Then
If String.IsNullOrEmpty(txtRefreshFiltra.Text) Then
txtRefreshFiltra.Text = ""
End If
Dim sum6 As String = Convert.ToInt32(e.Value)
If sum6 = txtRefreshFiltra.Text Then
e.CellStyle.BackColor = Color.Gold
e.CellStyle.ForeColor = Color.Black
End If
End If
Catch ex As Exception
MsgBox(ex.Message) 'show error msg'
End Try
End Sub
My connection
Public Sub FilterData(ValueToSearch As String)
Try
Dim SearchQyery As String = "SELECT * FROM LottoDeaBendata WHERE CONCAT_WS([Estratto1],[Estratto2],[Estratto3],[Estratto4],[Estratto5])LIKE'%" & ValueToSearch & "%'"
Dim command As New SqlCommand(SearchQyery, connection)
connection.Open()
Dim table As New DataTable()
Dim adapter As New SqlDataAdapter(command)
adapter.Fill(table)
DataGridView1.DataSource = table
connection.Close()
Catch ex As Exception
MsgBox(ex.Message) 'show error msg'
End Try
End Sub
Upload by button
Private Sub btnFiltraDati_Click(sender As Object, e As EventArgs) Handles btnFiltraDati.Click
FilterData(txtRefreshFiltra.Text)
End Sub
There are a few things you may want to consider to color the cells as you describe. First, using the grids CellFormatting event may not necessarily be the best choice. This event will fire once for each cell when the data is loaded into the grid and this is fine and colors the cells as we want when the data is loaded, however, it also may fire if the user simply moves the cursor over a cell or the user scrolls the grid.
In both the cases of the user moving the cursor over a cell or scrolling the grid, clearly demonstrates that the cells will get re-colored unnecessarily. In other words, if the text in the text box has not changed or a cells value has not changed, then, re-coloring the cell(s) is superfluous.
Given this, the only drawback to NOT using the grids CellFormatting event is that our code will have to color the cells AFTER the grid is loaded with data. This means we will need a method to loop through all the rows of the grid to check and color the cells. This method to color all the cells is also going to be needed if the text in the text box changes. So, making this method makes sense so we can call it when the data is loaded and also when the text box text changes.
So given all this, to simplify things, I suggest you create a method that takes a single DataGridViewCell. The method will get the comma separated values from the text box and compare the cells value to the values in the text box and if one matches, then we simply color the cell, otherwise do not color the cell.
This method is below. First, we check if the cell is not null and actually has some value. Then, we take the string in the text box and split it on commas. Then we start a loop through all the values in the split string from the text box and if a match is found, then we simply color the cell and exit the for each loop.
Private Sub ColorCell(cell As DataGridViewCell)
If (cell.Value IsNot Nothing) Then
Dim target = cell.Value.ToString()
If (Not String.IsNullOrEmpty(target)) Then
cell.Style.BackColor = Color.White
cell.Style.ForeColor = Color.Black
Dim split = txtRefreshFiltra.Text.Trim().Split(",")
For Each s As String In split
If (target = s.Trim()) Then
cell.Style.BackColor = Color.Gold
cell.Style.ForeColor = Color.Black
Exit For
End If
Next
End If
End If
End Sub
The method above should simplify looping through all the rows in the grid to color the proper cells. This method may look something like below and we would call this method once after the data is loaded into the grid and also when the text in the text box changes.
Private Sub ColorAllCells()
For Each row As DataGridViewRow In DataGridView1.Rows
ColorCell(row.Cells(3))
ColorCell(row.Cells(4))
ColorCell(row.Cells(5))
ColorCell(row.Cells(6))
ColorCell(row.Cells(7))
Next
End Sub
Lastly, the two event methods that we need to capture when the user changes a cells value in the grid in addition to when the user changes the text in the text box.
Private Sub txtRefreshFiltra_TextChanged(sender As Object, e As EventArgs) Handles txtRefreshFiltra.TextChanged
ColorAllCells()
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If (e.RowIndex >= 0) Then
If (e.ColumnIndex = 3 Or e.ColumnIndex = 4 Or e.ColumnIndex = 5 Or e.ColumnIndex = 6 Or e.ColumnIndex = 7) Then
ColorCell(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex))
End If
End If
End Sub
Edit per OP comment…
There are definitely a couple of things we can do to speed up the current code above, like take out the call to ColorAllCells in the text boxes TextChanged event.
The TextChanged event will fire when the user types a single character. Example, if the user wants to color the cells that are “55”, then, when the user types the first “5” into the text box… then the TextChanged event will fire and the code will color all the cells with “5”. Then when the user types the second “5”, the cells will be un-colored/colored again.
So, one way we can prevent the unnecessary coloring as described above is to NOT call the ColorAllCells method in the text boxes TextChanged event and simply put the ColorAllCells method into a button click. In other words, the user types what they want into the text box… THEN clicks a button to color the cells.
In addition, if you look at the ColorCell method, you may note that each time the method is called, the code is splitting the same string over and over with … Dim split = txtRefreshFiltra.Text.Trim().Split(",") … this is potentially redundant in a sense that the text … txtRefreshFiltra.Text may not have changed.
Therefore, to remedy this and only split the txtRefreshFiltra.Text when needed, we will use a global variable called something like currentSplit that holds the current split of the text box. Then we would “update” the currentSplit variable only when needed… like in its TextChanged event.
This should somewhat speed things up. In my small tests, it took approximately 10 seconds to color the cells the FIRST time. Subsequent coloring of the cells when the text box value was changed took less than 1 second.
First make a global variable to hold the current text boxes split values…
Dim currentSplit As String()
Then change the other methods as shown below…
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt = GetDT()
DataGridView1.DataSource = dt
currentSplit = txtRefreshFiltra.Text.Trim().Split(",")
End Sub
Private Sub ColorCell(cell As DataGridViewCell)
If (cell.Value IsNot Nothing) Then
Dim target = cell.Value.ToString()
If (Not String.IsNullOrEmpty(target)) Then
cell.Style.BackColor = Color.White
cell.Style.ForeColor = Color.Black
For Each s As String In currentSplit
If (target = s.Trim()) Then
cell.Style.BackColor = Color.Gold
cell.Style.ForeColor = Color.Black
Exit For
End If
Next
End If
End If
End Sub
Private Sub txtRefreshFiltra_TextChanged(sender As Object, e As EventArgs) Handles txtRefreshFiltra.TextChanged
currentSplit = txtRefreshFiltra.Text.Trim().Split(",")
End Sub
And finally, a button click event to color the cells. I added a stop watch to time how long it takes to color the cells.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sw As Stopwatch = New Stopwatch()
Debug.WriteLine("Starting coloring of cells -------")
sw.Start()
ColorAllCells()
sw.Stop()
Debug.WriteLine("It took: " + sw.Elapsed.TotalSeconds.ToString() + " to color the cells of 100,000 rows with 10 columns")
End Sub

vb.net change ForeColor in specific DataGridView cell

I want to change ForeColor in a specific DataGridView cell after edit.
I try to do this by:
Private Sub dgv_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgv_.CellEndEdit
dgv_.Item(e.RowIndex, e.ColumnIndex).Style.ForeColor = Color.Red
End Sub
but it's not working. What am I doing wrong?
You've got the indexes the wrong way around. When you index the Item property, it's column first, then row:
dgv_.Item(e.ColumnIndex, e.RowIndex).Style.ForeColor = Color.Red
You may have confused yourself because you do go row before column when you do it this way:
dgv_.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.ForeColor = Color.Red

Dynamically change a Control's ContextMenuStrip property at runtime?

I have a DataGridView that I want to use one ContextMenuStrip if a cell is right-clicked and a different ContextMenuStrip if a Cell Header is clicked. I DO NOT want to just add or remove or make visible or invisible members of the "same" CMS. I thought that it would be as easy as changing the DataGridView's property on a right-click, but I must be mistaken.
Private Sub DgvItems_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DgvItems.CellClick
Dim dgv As DataGridView = DgvItems
If MouseButtons.Right Then
Select Case md.HitTestInfo.Type
Case DataGridViewHitTestType.ColumnHeader
dgv.ContextMenuStrip = CmsDgvItemsColHdrs
dgv.ContextMenuStrip.Show()
Case DataGridViewHitTestType.Cell
dgv.ContextMenuStrip = CmsDgvItemsRows
dgv.ContextMenuStrip.Show()
End Select
End If
End Sub
Any ideas?
Duh. Maybe if I had the RIGHT EVENT the first time it would have worked.
Here's the proper code with NO need to "dgv.ContextMenuStrip.Show()" again...
Private Sub DgvItems_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DgvItems.CellMouseDown
Dim dgv As DataGridView = DgvItems
If MouseButtons.Right Then
Select Case md.HitTestInfo.Type
Case DataGridViewHitTestType.ColumnHeader
dgv.ContextMenuStrip = CmsDgvItemsColHdrs
Case DataGridViewHitTestType.Cell
dgv.ContextMenuStrip = CmsDgvItemsRows
End Select
End If
End Sub

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...

How to set the value of a DataGridView's EditingControl

I need to set the text value of a DataGridView.EditingControl. I've tried
myDGV.EditingControl.Text() = "1/1/2001"
and
myDGV.EditingControl.Text = "1/1/2001"
and
myDGV.EditingControl.Text("1/1/2001")
which causes an InvalidCastException (didn't check that could only be an integer).
There is no Value() property for the control, so how do I set the the value?
(Yes, I've verified that the cell is in edit mode)
You need to handle the EditingControlShowing event.
Private Sub HandleDgvEditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles Dgv.EditingControlShowing
If (TypeOf e.Control Is DataGridViewTextBoxEditingControl) Then
With DirectCast(e.Control, DataGridViewTextBoxEditingControl)
.Text = "1/1/2001"
End With
End If
End Sub
Note that this event will be fired for all editable cells. If you only want to manipulate the edit control for a given column you need to add another condition. Something like this:
If (Me.Dgv.CurrentCell.OwningColumn is Me.DgvFooColumn) Then
If you need to revert the edit from the CellValidating event then store the original value when the edit control is shown.
Private Sub HandleDgvEditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles Dgv.EditingControlShowing
'Cache the edit control text
Me.cachedEditText = e.Control.Text
End Sub
Private Sub HandleDgvCellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles Dgv.CellValidating
'Ensure that the edit control exists
If (Not Me.Dgv.EditingControl Is Nothing) Then
'Validate the edit
If (Not valid) Then
Me.Dgv.EditingControl.Text = Me.cachedEditText
End If
End If
End Sub
Private cachedEditText As String