c# - 在 datagridviewcombobox 中将 combobox.dropdownwidth 设置为最大宽度的问题
问题描述
我有一个 datagridview 组合框列。我需要将组合框的宽度动态调整为最长的字符串宽度,以便用户可以读取组合框项中的冗长字符串。我的代码是
int width = comboBox.DropDownWidth;
Graphics g = comboBox.CreateGraphics();
Font font = comboBox.Font;
int vertScrollBarWidth =
(comboBox.Items.Count > comboBox.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth;
foreach (string s in ((ComboBox)sender).Items)
{
newWidth = (int)g.MeasureString(s, font).Width
+ vertScrollBarWidth;
if (width < newWidth)
{
width = newWidth;
}
}
comboBox.DropDownWidth = width;
但这不起作用。我认为代码没有任何问题,但宽度仍然没有改变。我是否必须更改 datagridview 的任何属性才能更改宽度?
解决方案
推荐阅读
- android - 如何在不阻塞 UI 线程的情况下用 Room 中的数据填充微调器
- python - 高效的 Python 编程
- android - 使用 Dagger 2 注入数据绑定适配器
- c++ - 没有运算符“*”与 C++ 中的这些操作数匹配
- javascript - Typescript 对象索引器和与索引器类型不匹配的键
- c# - 从 .net 核心中的实体更新数据库架构(不使用实体框架迁移)
- algorithm - 在 2D 平面上寻找对象周围的路径
- cefsharp - 如何或应该如何将 OnAcceleratedPaint 用于 WPF
- android - Android androidx.recyclerview.widget.ListAdapter 如何知道所有更改何时完成?
- java - 如何正确从 Dropwizard 指标切换到千分尺?