Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET WindowsForms
I have a windows form with a DataGridView. I have columns in the grid that contain numbers I want to total and display in a label below the grid. The grid itself is set to resize column widths based on the data inside it, so at design time I don't know for sure where I should put the label/textbox. What is the best way to dynamically determine this?
 
The DataGridView's left is at 0. It has several columns:
colCombo, colItem, colSplitLink, colReported, colDetermined, ...(other columns that don't matter)
 
colCombo is a dropdown column, colSplitLink is a link column. colReported and colDetermined are numeric and have corresponding labels under them that contain total amounts. I also have a label with the text "Totals:" that should be just to the left of the labels that contain the totals. My code to set them in the proper location after the grid resizes is not quite right. The "Totals:" label looks a bit too far to the right, the Reported label looks a bit more too far to the right and the determined acres is very much too far to the right.
 
lblTotals.Left = colCombo.Width + colCombo.DividerWidth _
                 + colItem.Width + colItem.DividerWidth _
                 + colSplitLink.Width - lblTotals.Width
 
lblReported.Width = colReported.Width
 
lblReported.Left = lblTotals.Left + lblTotals.Width _ 
                   + colSplitLink.DividerWidth
 
lblDetermined.Width = colDetermined.Width
 
lblDetermined.Left = lblReported.Left + colReported.Width
 
I've tried it without adding the divider widths and also have tried using textboxes instead of labels for my totals, but it's still too far to the right. What am I missing? Since the problem gets worse for each label, I'm thinking it's something for each column that needs to be added/subtracted. But I don't know of anything other than the divider width to think about in this case. I don't want to randomly put in some value to subtract until it looks right. I want to be able to always calculate this dynamically and I just don't know what values I'm not taking into account.
Posted 13-Dec-12 6:27am
Kschuler32.6K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Okay...I tried the same code as above except instead of just directly referencing a column I referenced through the grid's .columns...for example, I used
 
dgvMyGrid.Columns(colCombo.Name).Width
intead of
colCombo.Width
 
And it seems to work okay. Not sure why. Don't care enough to look it up. I can't think of a reason why these methods should react differently or why anyone would want them to.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I had the same problem and to solve it, I intercept the ColumnWidthChanged event.
The width of the column txtLibel can changed, the others don’t change (sum of width = 530).
 
  Private Sub dgvEcrit2_ColumnWidthChanged(ByVal sender As Object, _
                             ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) _
                                Handles dgvEcrit2.ColumnWidthChanged
 
    If e.Column.Name = "txtLibel" Then
      Dim w As Int16 = dgvEcrit2.Columns("txtLibel").Width
      w = Me.dgvEcrit2.Location.X + Me.dgvEcrit2.RowHeadersWidth + 530 + w          
      Me.txtSoldeInitD.Location = New System.Drawing.Point(w + 5, 12)
      Me.txtSoldeInitC.Location = New System.Drawing.Point(w + 85, 12)
    End If
 
  End Sub
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 385
1 Sergey Alexandrovich Kryukov 262
2 OriginalGriff 200
3 George Jonsson 134
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,890
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100