DataGridViewComboBoxColumn 不使用自定义数据源呈现

发布于 2024-12-25 01:45:54 字数 1899 浏览 3 评论 0原文

使用此线程中的信息,我尝试设置并动态填充带有选项的 datagridviewcomboboxolumn 。我的调试器没有显示错误,但组合框不呈现。

//setup combobox
List<combo_automation_option> combo = new List<combo_automation_option>();
combo.Add(new combo_automation_option { id = "0", value = "None" });
combo.Add(new combo_automation_option { id = "*.0.5", value = "Once every 5 minutes" });

dgv_col_automation.HeaderText = "Automation";
dgv_col_automation.Name = "dgv_jobs_col_automation";
dgv_col_automation.FlatStyle = FlatStyle.Flat;
dgv_col_automation.DataSource = combo;
dgv_col_automation.ValueMember = "id";
dgv_col_automation.DataPropertyName = "value";

jobs_datagrid.Columns.Add(dgv_col_automation); 

当我使用下面的设置来设置组合框时,它呈现得很好:

//setup combobox                        
dt_col_automation.HeaderText = "Automation";
dt_col_automation.Name = "dgv_jobs_col_automation";
dt_col_automation.Items.AddRange(
        "Once every 5 minutes",
        "Once every 10 minutes",
        "Once every 15 minutes",
        "Once every 30 minutes",
        "Once every hour",
        "Once every 2 hours",
        "Once every 3 hours",
        "Once every 4 hours",
        "Once every 5 hours",
        "Once every 6 hours",
        "Once every 7 hours",
        "Once every 8 hours",
        "Once every 9 hours",
        "Once every 10 hours",
        "Once every 11 hours",
        "Once every 12 hours",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19",
        "20",
        "21",
        "22",
        "23",
        "24"
    );

我有一个看起来像这样的公共结构:

public struct combo_automation_option { 
   public string id;
   public string value;
}

有什么建议吗?

Using information from this thread I've attempted to setup and dynamically populate a datagridviewcomboboxolumn with options. My debugger shows no errors, but the combobox does not render.

//setup combobox
List<combo_automation_option> combo = new List<combo_automation_option>();
combo.Add(new combo_automation_option { id = "0", value = "None" });
combo.Add(new combo_automation_option { id = "*.0.5", value = "Once every 5 minutes" });

dgv_col_automation.HeaderText = "Automation";
dgv_col_automation.Name = "dgv_jobs_col_automation";
dgv_col_automation.FlatStyle = FlatStyle.Flat;
dgv_col_automation.DataSource = combo;
dgv_col_automation.ValueMember = "id";
dgv_col_automation.DataPropertyName = "value";

jobs_datagrid.Columns.Add(dgv_col_automation); 

It rendered fine when I used the settings below to setup my combo box:

//setup combobox                        
dt_col_automation.HeaderText = "Automation";
dt_col_automation.Name = "dgv_jobs_col_automation";
dt_col_automation.Items.AddRange(
        "Once every 5 minutes",
        "Once every 10 minutes",
        "Once every 15 minutes",
        "Once every 30 minutes",
        "Once every hour",
        "Once every 2 hours",
        "Once every 3 hours",
        "Once every 4 hours",
        "Once every 5 hours",
        "Once every 6 hours",
        "Once every 7 hours",
        "Once every 8 hours",
        "Once every 9 hours",
        "Once every 10 hours",
        "Once every 11 hours",
        "Once every 12 hours",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19",
        "20",
        "21",
        "22",
        "23",
        "24"
    );

I've a public struct that looks like this:

public struct combo_automation_option { 
   public string id;
   public string value;
}

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2025-01-01 01:45:54

数据绑定不适用于类/结构字段,即使它们是公共的。

您应该使用属性,例如:

public struct combo_automation_option { 
   public string id {get; set;}
   public string value {get; set;}
}

此外,如果您想显示其他内容(例如值)而不是组合框中的 id,还可以设置 DisplayMember 属性。

顺便说一句,其他一些建议:

  • 仅在确实必要时才应使用结构;使用类而
  • 不是遵循通常的 C# 命名约定,即:始终采用驼峰式大小写(无下划线),并且只有结构/类、方法、属性必须以大写字母开头。

Data binding doesn't work on class/struct fields even if thery're public.

You should use properties instead e.g.:

public struct combo_automation_option { 
   public string id {get; set;}
   public string value {get; set;}
}

Furthermore, if you want to show something else (e.g. the value) instead of the id in the combobox, set also the DisplayMember property.

BTW, other few suggestions:

  • structs should be used only if really necessary; use classes instead
  • you're not following the usual c# naming convenction that is: always camel casing (no underscore) and only struct/classes, methods, properties must start with the a capital letter.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文