Click here to Skip to main content
12,945,066 members (57,109 online)
Rate this:
Please Sign up or sign in to vote.
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
Rate this: bad
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


intead of

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.
Rate this: bad
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

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,781
CHill60 3,135
Maciej Los 2,443
Jochen Arndt 1,910
ppolymorphe 1,765

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 10 Jun 2015
Copyright © CodeProject, 1999-2017
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