【问题标题】:Collection Items ComboBox in Datagrid C#Datagrid C#中的集合项组合框
【发布时间】:2011-07-31 11:42:40
【问题描述】:

我想用 C# 制作一个 datagridview1

这有 3 行 (Number,Name and Country)

如果我想在datagridview1 中提供combobox

上排是(Number,Name and Country)的标题或描述

下面是............一个选定的组合框下拉菜单

数字组合框有5个选择或5个收藏项("1","2","3","4","5")

名称组合框有 5 个选项或 5 个收藏项("A","B","C","D","E")

国家/地区组合框有 5 个选择或 5 个收藏项("Amerika","England","Indonesia","Australia","India")

你能帮我怎么做吗???

【问题讨论】:

    标签: c# winforms datagridview combobox


    【解决方案1】:

    每一列的类型必须是DataGridViewComboBoxColumn.

    然后,此列类型会公开各种属性,使您可以正确设置列组合框选项。

    对于您要求的非常简单的情况,您可以执行以下操作(仅在这里说国家列):

    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
    col.HeaderText = "Country";
    col.Name = "Country";
    col.Items.AddRange("Amerika","England","Indonesia","Australia","India");
    
    dataGridView1.Columns.Add(col);
    

    现在您的网格中有一个组合框列,其中包含可用的国家/地区。

    还有许多其他选项可为您提供更大的灵活性和功能。例如,如果您的 DataGridView 有一个包含“ChosenCountry”属性的 DataSource,您可以在列和 DataSource 之间设置绑定。

    col.DataPropertyName = "ChosenCountry";
    

    再进一步,您可以为这些国家/地区使用 id 而不是依赖纯文本:

    List<Country> countries = new List<Country>();
    countries.Add(new Country { Id = 0, Name = "Amerkia" });
    countries.Add(new Country { Id = 1, Name = "England" });
    // and so on or even better get this from some external store using a query
    
    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
    
    // Now you can use the CheckBoxColumn datasource:
    col.DataSource = countries;
    // And you set the Display and value members and the DataPropertyName:
    col.DisplayMember = "Name";
    col.ValueMember = "Id";
    col.DataPropertyName = "CountryId";
    

    除此之外,您需要提出问题,详细说明您的确切场景和问题,以获得更具体的答案。

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 2014-12-12
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 2017-08-20
      • 1970-01-01
      相关资源
      最近更新 更多