Results 1 to 6 of 6

Thread: [2005] 4 Grids - One Sub

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    [2005] 4 Grids - One Sub

    I have 4 DataGridView controls on my form. The grids are named grdG1, grdG2, grdG3 and grdG4. (Remember that's 1, 2, 3, and 4) I want to control the ColumnAdded sub for all of them in the same code block. The columns are added at runtime and need to be a certain width depending on which column index it is. The first two columns are wide and all the rest should be 40 pixels wide. I also need to set the ForeColor for each new column depending on the value in my Course.Assignment class.

    The code below works for grdG1, and I could just copy it 3 more times and changed the numbers, but I think there should be a way to do it with the (ByVal sender).name and CType or DirectCast (neither of which I fully understand yet) and get the number from the grid name. Actually, I need to be able to do stuff in the rest of the program depending on which grd has been clicked, edited, and so on.

    vb Code:
    1. Private Sub grd_ColumnAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles grdG1.ColumnAdded ', grdG2.ColumnAdded
    2.  
    3.         Dim i As Integer = e.Column.Index
    4.         Select Case i
    5.             Case 0
    6.                 grdG1.Columns(0).Width = DefaultNameColumnWidth
    7.                 grdG1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
    8.                 grdG1.Columns(0).Frozen = True
    9.                 'grdG1.Columns(0).DefaultCellStyle = Font.helpme
    10.             Case Else
    11.                 grdG1.Columns(i).Width = 40
    12.                 grdG1.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    13.  
    14.                 Select Case Trim(Course.Assignment(1, i).GradeType)
    15.                     Case "Test"
    16.                         grdG1.Columns(i).DefaultCellStyle.ForeColor = Color.Red
    17.                     Case "Homework"
    18.                         grdG1.Columns(i).DefaultCellStyle.ForeColor = Color.Black
    19.                     Case "9 Weeks Test"
    20.                         grdG1.Columns(i).DefaultCellStyle.ForeColor = Color.Blue
    21.                     Case "Semester Test"
    22.                         grdG1.Columns(i).DefaultCellStyle.ForeColor = Color.Green
    23.                 End Select
    24.         End Select
    25.     End Sub

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] 4 Grids - One Sub

    Oh and I tried this... but the form wouldn't even load so something's wrong.

    vb Code:
    1. Dim grd As DataGridView = CType(sender, DataGridView)
    2.         Dim nw As Integer = Integer.Parse(Microsoft.VisualBasic.Right(grd.Name, 1))
    3.  
    4.         Dim i As Integer = e.Column.Index
    5.         Select Case i
    6.             Case 0
    7.                 grd.Columns(0).Width = DefaultNameColumnWidth
    8.                 grd.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
    9.                 grd.Columns(0).Frozen = True
    10.                 'grd.Columns(0).DefaultCellStyle = Font.helpme
    11.             Case Else
    12.                 grd.Columns(i).Width = 40
    13.                 grd.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    14.  
    15.                 Select Case Trim(Course.Assignment(nw, i).GradeType)
    16.                     Case "Test"
    17.                         grd.Columns(i).DefaultCellStyle.ForeColor = Color.Red
    18.                     Case "Homework"
    19.                         grd.Columns(i).DefaultCellStyle.ForeColor = Color.Black
    20.                     Case "9 Weeks Test"
    21.                         grd.Columns(i).DefaultCellStyle.ForeColor = Color.Blue
    22.                     Case "Semester Test"
    23.                         grd.Columns(i).DefaultCellStyle.ForeColor = Color.Green
    24.                 End Select
    25.         End Select

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] 4 Grids - One Sub

    The 'sender' parameter is always a reference to the object that raised the event. It will always refer to the correct DGV. You simply need to cast it as the correct type to access its members.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] 4 Grids - One Sub

    Isn't that what I did in line 1 of the 2nd code I posted?

    But something there is killing the form before it gets loaded.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] 4 Grids - One Sub

    Your cast is correct, although I'd suggest DirectCast over CType. If the form won't load it's nothing to do with the cast. Remove the rest of the code and start adding it back bit by bit until it stops working. Then you know what the problem is. Problem solving techniques in programming are no different to problem solving techniques in other areas.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] 4 Grids - One Sub

    Presumably columns are being added during the creation of the form and some of that code is invalid at that point. Put a breakpoint at the top of the code and then step through to see where the issue is. I think a breakpoint in an event handler should be hit during creation.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width