在特定DataGridView列下对齐标签 [英] Align label under a specific DataGridView column

查看:55
本文介绍了在特定DataGridView列下对齐标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有DataGridView的窗体。我在网格中有列包含我想要的数字,并显示在网格下方的标签中。网格本身设置为根据其中的数据调整列宽,因此在设计时我不确定应该放置标签/文本框的位置。动态确定这个的最佳方法是什么?



DataGridView的左边是0.它有几列:

colCombo,colItem,colSplitLink,colReported,colDetermined,...(其他列没关系)



colCombo是一个下拉列,colSplitLink是一个链接列。 colReported和colDetermined是数字,并在其下面包含包含总金额的相应标签。我还有一个带有文本Totals:的标签,它应该位于包含总计的标签的左侧。我在网格调整大小后将它们设置在适当位置的代码并不完全正确。 Totals:标签看起来有点偏右,报告标签看起来有点偏右,确定的英亩距离右边太远了。



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.

推荐答案

好的......我试过了与上面相同的代码,除了直接引用我通过网格的.columns引用的列...例如,我使用



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


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.


我有同样的问题并解决它,我拦截了ColumnWidthChanged事件。 br />
列txtLibel的宽度可以改变,其他的不改变(宽度= 530的总和)。



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


这篇关于在特定DataGridView列下对齐标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆