简体   繁体   中英

Dynamic resizing of DataGrid according to other control

I have 2 controls: dataGridView and Label. I load some files into datagrid and I show names of files in 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. 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). 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? 该图显示了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. 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.

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; if (bindableText.Length > maxLength) { label.Tooltip = bindableText; 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) + "...";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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