Wednesday, April 30, 2014

Tip of the Day

When working with column names in your DataGridView, always refer to them as Column.Name, rather than "ColumnName".

Example:

Select Case colCheckBox
            Case "colApprovedByExecutive"
                If desiredCheckboxStatus = True Then
                    cellDate.Value = DateTime.Now().ToString()
                    cellUsername.Value = GetLoggedInUser()
                Else
                    cellDate.Value = New DateTime(1753, 1, 1)
                    cellUsername.Value = String.Empty
                End If
End Select  
Simply, don't do this. When you remove columns, rename them, or whatever, you'd have to search through the solution for every last one. Instead, use:


Case colApprovedByExecutive.Name     ...

Since you're just referencing the actual columns, you will only get a compilation error. That is *much* more visible than the alternative and will make your code easier to work with.

P.S.: My recent posts may have had nothing to do with C#. I will probably be moving over to a more IT/General Development style. I will also be posting more tidbits such as this. :)

No comments:

Post a Comment