|
-
Jul 12th, 2007, 02:46 AM
#1
Thread Starter
Hyperactive Member
[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:
Private Sub grd_ColumnAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles grdG1.ColumnAdded ', grdG2.ColumnAdded
Dim i As Integer = e.Column.Index
Select Case i
Case 0
grdG1.Columns(0).Width = DefaultNameColumnWidth
grdG1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
grdG1.Columns(0).Frozen = True
'grdG1.Columns(0).DefaultCellStyle = Font.helpme
Case Else
grdG1.Columns(i).Width = 40
grdG1.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
Select Case Trim(Course.Assignment(1, i).GradeType)
Case "Test"
grdG1.Columns(i).DefaultCellStyle.ForeColor = Color.Red
Case "Homework"
grdG1.Columns(i).DefaultCellStyle.ForeColor = Color.Black
Case "9 Weeks Test"
grdG1.Columns(i).DefaultCellStyle.ForeColor = Color.Blue
Case "Semester Test"
grdG1.Columns(i).DefaultCellStyle.ForeColor = Color.Green
End Select
End Select
End Sub
-
Jul 12th, 2007, 02:47 AM
#2
Thread Starter
Hyperactive Member
Re: [2005] 4 Grids - One Sub
Oh and I tried this... but the form wouldn't even load so something's wrong.
vb Code:
Dim grd As DataGridView = CType(sender, DataGridView)
Dim nw As Integer = Integer.Parse(Microsoft.VisualBasic.Right(grd.Name, 1))
Dim i As Integer = e.Column.Index
Select Case i
Case 0
grd.Columns(0).Width = DefaultNameColumnWidth
grd.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
grd.Columns(0).Frozen = True
'grd.Columns(0).DefaultCellStyle = Font.helpme
Case Else
grd.Columns(i).Width = 40
grd.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
Select Case Trim(Course.Assignment(nw, i).GradeType)
Case "Test"
grd.Columns(i).DefaultCellStyle.ForeColor = Color.Red
Case "Homework"
grd.Columns(i).DefaultCellStyle.ForeColor = Color.Black
Case "9 Weeks Test"
grd.Columns(i).DefaultCellStyle.ForeColor = Color.Blue
Case "Semester Test"
grd.Columns(i).DefaultCellStyle.ForeColor = Color.Green
End Select
End Select
-
Jul 12th, 2007, 02:49 AM
#3
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.
-
Jul 12th, 2007, 03:05 AM
#4
Thread Starter
Hyperactive Member
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.
-
Jul 12th, 2007, 03:26 AM
#5
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.
-
Jul 12th, 2007, 03:30 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|