Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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

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

VB
dgvMyGrid.Columns(colCombo.Name).Width

intead of
VB
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.
 
Share this answer
 
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900