gpt4 book ai didi

c# - 设置 DataGridView ComboBoxColumn 的 DropDown 列表宽度 - WinForms

转载 作者:太空狗 更新时间:2023-10-30 00:57:59 25 4
gpt4 key购买 nike

我有一个带有组合框列的数据 GridView 。此列是数据绑定(bind)的。我想根据列表中最大的项目宽度设置下拉列表的宽度。为了让普通的组合框达到同样的效果,我使用了一种扩展方法,该方法将通过查找列表中最大宽度的项目来设置组合框的宽度。这是在组合框的 DropDown 事件中完成的。

现在在 DataGridView 组合框列中,我想实现相同的目的。在这种情况下如何获得 DropDown 事件?如果有任何其他方法可以实现相同的目标,请告诉我?

最佳答案

经过一些调查,我找到了这个问题的答案。

我将数据源设置为数据 GridView 的组合框列。因此,在设置数据源后,我找到了数据表中最大项目的宽度,该值设置为列的 DisplayMember。我在我的问题中使用上面给出的链接中提到的相同逻辑,而不是在 DropDown 事件中做,我在设置数据源时这样做,这是一次性的。在我的问题上面给出的链接中,每次显示下拉列表时都设置下拉列表的宽度。所以,在某种程度上,我的方法看起来不错。

在这里,我是如何做到的:

// This line is picked up from designer file for reference
DataGridViewComboBoxColumn CustomerColumn;

DataTable _customersDataTable = GetCustomers();

CustomerColumn.DataSource = _customersDataTable;
CustomerColumn.DisplayMember = Customer_Name;
CustomerColumn.ValueMember = ID;

var graphics = CreateGraphics();

// Set width of the drop down list based on the largest item in the list
CustomerColumn.DropDownWidth = (from width in
(from DataRow item in _customersDataTable.Rows
select Convert.ToInt32(graphics.MeasureString(item[Customer_Name].ToString(), Font).Width))
select width).Max();

关于c# - 设置 DataGridView ComboBoxColumn 的 DropDown 列表宽度 - WinForms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3905696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com