简体   繁体   English

根据其他控件动态调整DataGrid的大小

[英]Dynamic resizing of DataGrid according to other control

I have 2 controls: dataGridView and Label. 我有2个控件:dataGridView和Label。 I load some files into datagrid and I show names of files in Label. 我将一些文件加载​​到datagrid中,并在Label中显示文件名。 Now i am dynamically changing maximum width of Label according to size of window but what can I do if I want to work with maximum height. 现在,我正在根据窗口大小动态更改Label的最大宽度,但是如果我想使用最大高度,该怎么办。 I mean is there anyway how can I resize datagrid below Label if Label overfloating datagrid (some feature that say that these 2 controls can´t "overfloating" - i am sorry, I don´t know better english word for this). 我的意思是,无论如何,如果Label浮动datagrid,我该如何在Label下调整datagrid的大小(某些功能说这两个控件不能“浮动”-很抱歉,我对此不知道更好的英语单词)。 Or is there a way how can I add 3 dots at end of label and rest of content in label is show when I get mouse over it? 还是有一种方法,当我将鼠标悬停在标签末尾时,如何在标签末尾添加3个点,并显示标签中的其余内容? 该图显示了datagridview上方的“ overfloating”标签

Thank you 谢谢

For the text shortening, use: 对于文本缩短,请使用:

Label myLabel = new Label();
myLabel.Location = new System.Drawing.Point(10, 10);
myLabel.Size = new System.Drawing.Size(100, 15);
myLabel.AutoEllipsis = true;
myLabel.Text = "Some Text That Will Be Ellipsed";

Full length article can be found here . 完整的文章可以在这里找到。

Use a TableLayoutPanel to create dynamic flowing layouts. 使用TableLayoutPanel创建动态的流动布局。 You can 'dock' the label to one cell of the layout, and let it grow automatically when the label grows. 您可以将标签“停靠”到布局的一个单元格,并在标签增长时使其自动增长。 The DataGrid will automatically be resized and repositioned. DataGrid将自动调整大小并重新定位。

Unless you want the size of your grid to be fixed for data display purposes, a flowing layout is probably the way to go. 除非您为数据显示目的而固定网格的大小,否则可能要采用顺畅的布局。

However, you could modify your binding code so that if your label text is longer than some decided amount of characters you can store the full text in the Tooltip and do a .Remove on everything beyond that character count. 但是,您可以修改绑定代码,以便如果标签文本的长度超过某些确定的字符数,则可以将全文存储在工具提示中,并对所有超出该字符数的内容进行删除。 Probably want to append an ellipsis on the label text also. 可能还要在标签文本上加上省略号。 Something like: 就像是:

int maxLength = 1000; int maxLength = 1000; if (bindableText.Length > maxLength) { label.Tooltip = bindableText; 如果(bindableText.Length> maxLength){label.Tooltip = bindableText; label.Text = bindableText.Remove(maxLength) + "..."; label.Text = bindableText.Remove(maxLength)+“ ...”; } }

Or you can measure the pixel of your title and dynamically modify it : 或者,您可以测量标题的像素并对其进行动态修改:

System.Drawing.Graphics myG = Graphics.FromImage(new Bitmap(1, 1));
int numberPixel = myG.MeasureString(myTitle, myFontTitle);

if (numberPixel > XXX)
{
   myTitle = myTitle.Substring(0,YYY) + "...";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM