C1FlexGrid - Change "Highlight" behavior for a range of cells - vb.net

My scenario:
I have a C1FlexGrid with Column and Row headers. Additionally, the first row of the C1FlexGrid is a boolean row (checkboxes). Based on the state of these checkboxes, the desired effect is a disabling of the cells for that column. The difficulty is that, due to the first row being a boolean data type that must not be disabled, using the .Cols(index).AllowEditing property is not an option. I have already successfully implemented a work-around using the BeforeEdit event handler, to prohibit changes to the cells in the column, along with a CellStyle to grey-out the cells when disabled.
Private Sub C1FlexGrid1_BeforeEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles C1FlexGrid1.BeforeEdit
If e.Row > 1 And Me.C1FlexGrid1.Item(1, e.Col) = False Then e.Cancel = True
End Sub
Private Sub C1FlexGrid1_AfterEdit(ByVal sender As Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles C1FlexGrid1.AfterEdit
If e.Row = 1 And Me.C1FlexGrid1.Item(1, e.Col) = False Then
Call FormatColAsDisabled(e.Col)
ElseIf e.Row = 1 And Me.C1FlexGrid1.Item(1, e.Col) = True Then
Call FormatColAsEnabled(e.Col)
End If
End Sub
Private Sub FormatColAsDisabled(ByVal col As Integer)
Dim color As C1.Win.C1FlexGrid.CellStyle
color = Me.C1FlexGrid1.Styles.Add("Gray")
color.BackColor = Drawing.Color.Gray
For row As Integer = 2 To Me.C1FlexGrid1.Rows.Count - 1
Me.C1FlexGrid1.SetCellStyle(row, col, color)
Next
End Sub
Private Sub FormatColAsEnabled(ByVal col As Integer)
For row As Integer = 2 To Me.C1FlexGrid1.Rows.Count - 1
Me.C1FlexGrid1.SetCellStyle(row, col, Me.C1FlexGrid1.Styles("Normal"))
Next
End Sub
My Question
Is there a way to modify the "Highlight" behavior for only these cells, such that the disabled cells do not highlight at all?

In order to avoid highlighting the disabled cells you can set Cancel EventArgs of BeforeSelChange event to true in case the new selected cell is a disabled one.
Private Sub C1FlexGrid1_BeforeSelChange(sender As Object, e As RangeEventArgs) Handles C1FlexGrid1.BeforeSelChange
If e.NewRange.r1 > 1 And Me.C1FlexGrid1.GetCellCheck(1, e.NewRange.c1) = CheckEnum.Unchecked Then
e.Cancel = True
End If
End Sub

Related

Add CheckBox To DataGridView Cell Programmatically

I’m trying to add a CheckBox programmatically to a DataGridVew cell if the cell next to it has a value of “1”. I’m trying to do this as the rows are added
I’m hoping someone can help me out with the correct code here. I understand one of the lines of code is incorrect but I’ve put it in to illustrate what I'm trying to do.
Thanks in advance.
Private Sub Controls_DGV_RowsAdded(sender As Object, e As Windows.Forms.DataGridViewRowsAddedEventArgs) Handles Controls_DGV.RowsAdded
If Controls_DGV.Rows(e.RowIndex).Cells(2).Value = "1" Then
Controls_DGV.Rows(e.RowIndex).Cells(1).AddCheckBox ' THIS LINE IS INCORRECT
End If
End Sub
This is the same as #miguel except for the checking the value, in this case Option Strict is On as it should be.
Public Class Form1
Private Sub dataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) _
Handles dataGridView1.RowsAdded
If CStr(dataGridView1.Rows(e.RowIndex).Cells(1).Value) <> "1" Then
dataGridView1.Rows(e.RowIndex).Cells(0).Value = False
dataGridView1.Rows(e.RowIndex).Cells(0) = New DataGridViewTextBoxCell()
dataGridView1.Rows(e.RowIndex).Cells(0).Value = ""
dataGridView1.Rows(e.RowIndex).Cells(0).ReadOnly = True
End If
End Sub
Private Sub AddRowsButton_Click(sender As Object, e As EventArgs) _
Handles AddRowsButton.Click
For index As Integer = 0 To 5
If CBool(index Mod 2) Then
dataGridView1.Rows.Add(False, "0")
Else
dataGridView1.Rows.Add(False, "1")
End If
Next
End Sub
End Class
The column number 1 that you want to display a checkbox should already be of type DataGridViewCheckBoxColumn and then if the value is not "1" you can transform the type of the cell for a DataGridViewTextBoxCell, so there is no checkbox and you can even put there some text if you want. Because you're using 3 columns, i'll try to do the same.
In your Form1_Load() you should have something like this if you are adding columns programmatically:
Dim ChkBox As New DataGridViewCheckBoxColumn
Controls_DGV.Columns.Add("TextBox1", "TextBox1")
Controls_DGV.Columns.Add(ChkBox)
Controls_DGV.Columns.Add("TextBox2", "TextBox2")
Then using your code it should be like this:
Private Sub Controls_DGV_RowsAdded(sender As Object, e As Windows.Forms.DataGridViewRowsAddedEventArgs) Handles Controls_DGV.RowsAdded
If Controls_DGV.Rows(e.RowIndex).Cells(2).Value <> "1" Then
' replace the checkbox cell by textbox cell
Controls_DGV.Rows(e.RowIndex).Cells(1) = New DataGridViewTextBoxCell()
Controls_DGV.Rows(e.RowIndex).Cells(1).Value = "(empty or some text)"
End If
End Sub

Change row color on CheckBox check

I have a DataGridView with multiple columns and the first one is a CheckBox column. What I want is, when a user checks any row's checkbox, the row color will change (any color). This is what I've tried so far:
Private Sub dataGridView1_CellValueChanged(sender As Object, e As
DataGridViewCellEventArgs)
If DataGridView1.Columns(e.ColumnIndex).Name = "ColCheck" Then
If DataGridView1.Rows(e.RowIndex).Cells("ColCheck").Value = True Then
Dim isChecked As Boolean = DirectCast(dataGridView1(e.ColumnIndex, e.RowIndex).FormattedValue, [Boolean])
If isChecked Then
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightGreen
End If
End If
End If
End Sub
But it doesn't work. Any solution?

How to make cell readonly if the second cell is empty

The user should not be able to input a qty where the unit is empty in the datagridview.
To make it clear, I want to make the cell readonly = true if unit column is empty.
The colUOM4 is the name of the column that if the cell of this column is empty the olNewQty2 cell will be readonly.
I tried this code but it didn't work
Public Sub UnitEmpty()
For i As Integer = 0 To dgvCount.RowCount - 1
If dgvCount.Rows(i).Cells("colUOM4").Value Is Nothing Then
MessageBox.Show("It worked!")
dgvCount.Rows(i).Cells("colNewQty2").ReadOnly = True
Else
MessageBox.Show("Nothing happened!")
Exit For
End If
Next
End Sub
I'd recommend not using a loop because that will only set the state when you execute it and not react to any changes. I'd suggest working at the row and cell level, i.e. set the default state when a row is added and then react when a specific cell changes, e.g.
Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
For i = e.RowIndex To e.RowIndex + e.RowCount - 1
'Make the first cell in each new row read-only by default.
DataGridView1(0, i).ReadOnly = True
Next
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
'Check whether the change is in the second column.
If e.RowIndex >= 0 AndAlso e.ColumnIndex = 1 Then
Dim row = DataGridView1.Rows(e.RowIndex)
'Make the first cell in the row read-only if and only if the second cell is empty.
row.Cells(0).ReadOnly = (row.Cells(1).Value Is Nothing)
End If
End Sub

Combo Box Returning -1 For SelectedIndex

I'm trying to pick up a combo box's selected index. This was working absolutely fine, then all of a sudden it started returning -1 no matter what item is selected
My code is:
Form Code
Private Sub Man_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles Man.SelectedIndexChanged, Units.SelectedIndexChanged
'Set Transducer Type
Call References.LevListAdd()
End Sub
References Module LevListAdd Sub
Public Sub LevListAdd()
Form1.Lev.Items.Clear()
If Form1.Man.Text = "Pulsar" Then
With Form1.Lev.Items
.Add("Ultra Range")
.Add("IMP Range")
.Add("Twin Range")
End With
End If
End Sub
This fills the combo box lev fine when the Man combo box item "Pulsar" is selected. I then want my users to click a button to generate some labels and stuff. The code is as such:
Button Click Code
Private Sub Generate_Click(sender As Object, e As EventArgs) Handles Generate.Click
If CheckGenerate() = False Then Exit Sub
Dim X = CheckGenerationType(Man.SelectedIndex, Lev.SelectedIndex, Level.Checked, Volume.Checked, ListBox1.SelectedIndex,
Units.SelectedIndex)
Call ParamPage(X)
End Sub
CheckGenerate simply checks that all boxes have been filled in. I pass the informtion from the form to the following function:
Public Function CheckGenerationType(Man As Integer, Lev As Integer, Level As Boolean, Volume As Boolean, TankType As Integer,
MeasurementUnit As Integer) As String
Dim M As Integer
Dim L As Integer
Dim CT As Integer
Dim TT As Integer
Dim Ms As Integer
M = Man
L = Lev
TT = TankType
Ms = MeasurementUnit
If Level = True Then
CT = 0
ElseIf Volume = True Then
CT = 1
End If
CheckGenerationType = CStr(M) + "." + CStr(L) + "." + CStr(CT) + "." + CStr(TT) + "." + CStr(Ms)
End Function
When the lev.selectedindex parameter arrives at this function, it reads -1. Even if the user has selected any of the 3 items. Can anyone explain why this is happening?
I've just tried your code. I get the same result (-1 in lev.SelectedIndex) and this was because jumped using tab through the controls my when i'm hitting the Man or Units Combobox it runs the LevListAdd() and then clears the Lev-ComboBox because of Form1.Lev.Items.Clear().
You should think about your call Man_SelectedIndexChanged_1 or maybe just use something like this:
Public Sub LevListAdd()
If Form1.Man.Text = "Pulsar" Then
Form1.Lev.Items.Clear()
instead of
Public Sub LevListAdd()
Form1.Lev.Items.Clear()
If Form1.Man.Text = "Pulsar" Then
And you should separate the calls from the man and unit ComboBoxes.
Private Sub Unit_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles Units.SelectedIndexChanged
' Do something
End Sub
Private Sub Man_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles Man.SelectedIndexChanged
Form1.Lev.Items.Clear()
Select Case Form1.Man.Text
Case "Pulsar"
With Form1.Lev.Items
.Add("Ultra Range")
.Add("IMP Range")
.Add("Twin Range")
End With
Case "animals"
With Form1.Lev.Items
.Add("dogs")
.Add("cats")
.Add("birds")
End With
End Select
End Sub

Change datagridview cell background based on a external parameter

I should change the color to a cell which contains the parameter 'tarjeta_fam'. I tried to change the cell default property and then invalidate the row to refresh it, but (obviously) nothing happens. It's possible to change a cell color out of the cell formatting event?
Public Sub New(user As Usuario, ByVal tarjeta_fam As String)
InitializeComponent()
gridFamiliares.DataSource = BD.getTable(a query)
If Me.gridFamiliares.Rows.Count > 0 Then
For i As Integer = 0 To Me.gridFamiliares.Rows.Count - 1
If Me.gridFamiliares.Rows(i).Cells("tarjeta_fam").Value = tarjeta_fam Then
Me.gridFamiliares.Rows(i).DefaultCellStyle.BackColor = Color.Black
Me.gridFamiliares.InvalidateRow(i)
End If
Next
End If
End Sub
The DataGridView control really wants you to use the CellFormatting event for this, so declare a form level variable to be used by that event:
Private tarjeta_fam_Value As String = String.Empty
Public Sub New(user As Usuario, ByVal tarjeta_fam As String)
InitializeComponent()
gridFamiliares.DataSource = BD.getTable(a query)
tarjeta_fam_Value = tarjeta_fam
End Sub
Private Sub gridFamiliares_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles gridFamiliares.CellFormatting
If tarjeta_fam_Value <> String.Empty Then
With gridFamiliares.Rows(e.RowIndex)
If .Cells("tarjeta_fam").Value = tarjeta_fam_Value Then
.DefaultCellStyle.BackColor = Color.Black
End If
End With
End If
End Sub