I have an Expense Data Entry form which contains a DataGrid showing various
expense categories. There are three columns Description, Cash Exp, Credit
Exp, where Description column is readonly.
Users have to press a checkbox to specify whether they want to use DataGrid
to provide break-up or provide total value in textbox. Now when checkbox is
unchecked I am setting the Enabled property of DataGrid to False and vice
versa. The problem is when enabled property is set back to True, the
scrollbar disappears.
Secondly, I am using TableStyle in order to change the color of datagrid to
show whether it is enabled or disabled. The problem if I click a cell in
Description column and then set the checkbox to uncheck, it does not changes
the color of the clicked cell. It leaves the color same as the previous one.
This is happening only for Description column. I tried changing readonly
property of description column to false and it worked fine. It is just giving
an ugly look cos if I press more than one cell before un-checking it leaves
those cells as previous color and changes color for all others.
What could be going wrong? How can I fix it? Below is the code I am calling
on checkbox click event:
Private Sub EnableDataGrid(ByVal enabled As Boolean)
grdAExpenses.TableStyles.Clear()
Select Case enabled
Case True
grdAExpenses.BackgroundColor = Color.FromArgb(192, 255, 192)
Dim ts As New DataGridTableStyle
ts.MappingName = "ClientExpenses"
ts.AllowSorting = False
ts.RowHeadersVisible = False
ts.BackColor = Color.FromArgb(192, 255, 192)
ts.AlternatingBackColor = Color.FromArgb(192, 255, 192)
Dim cs As New DataGridTextBoxColumn
cs.MappingName = "Description"
cs.HeaderText = "Description"
cs.ReadOnly = True
cs.Width = 115
cs.TextBox.BackColor = Color.FromArgb(192, 255, 192)
ts.GridColumnStyles.Add(cs)
Dim noCol As New DataGridNumericTextBoxColumn
noCol.MappingName = "CashExpenditure"
noCol.HeaderText = "Cash Expenditure"
noCol.Format = "c"
noCol.Width = 100
noCol.Alignment = HorizontalAlignment.Right
noCol.TextBox.BackColor = Color.FromArgb(192, 255, 192)
ts.GridColumnStyles.Add(noCol)
noCol = New DataGridNumericTextBoxColumn
noCol.MappingName = "CreditExpenditure"
noCol.HeaderText = "Credit Expenditure"
noCol.Format = "c"
noCol.Width = 100
noCol.Alignment = HorizontalAlignment.Right
noCol.TextBox.BackColor = Color.FromArgb(192, 255, 192)
ts.GridColumnStyles.Add(noCol)
Me.grdAExpenses.TableStyles.Add(ts)
Me.grdAExpenses.Enabled = True
Case False
grdAExpenses.BackgroundColor = SystemColors.Control
Dim ts As New DataGridTableStyle
ts.MappingName = "ClientExpenses"
ts.AllowSorting = False
ts.RowHeadersVisible = False
ts.BackColor = SystemColors.Control
ts.AlternatingBackColor = SystemColors.Control
Dim cs As New DataGridTextBoxColumn
cs.MappingName = "Description"
cs.HeaderText = "Description"
cs.ReadOnly = True
cs.Width = 115
cs.TextBox.BackColor = SystemColors.Control
ts.GridColumnStyles.Add(cs)
Dim noCol As New DataGridNumericTextBoxColumn
noCol.MappingName = "CashExpenditure"
noCol.HeaderText = "Cash Expenditure"
noCol.Format = "c"
noCol.Width = 100
noCol.Alignment = HorizontalAlignment.Right
noCol.TextBox.BackColor = SystemColors.Control
ts.GridColumnStyles.Add(noCol)
noCol = New DataGridNumericTextBoxColumn
noCol.MappingName = "CreditExpenditure"
noCol.HeaderText = "Credit Expenditure"
noCol.Format = "c"
noCol.Width = 100
noCol.Alignment = HorizontalAlignment.Right
noCol.TextBox.BackColor = SystemColors.Control
ts.GridColumnStyles.Add(noCol)
Me.grdAExpenses.TableStyles.Add(ts)
Me.grdAExpenses.Enabled = False
End Select
End Sub