【问题标题】:How to fill only Selected cells of GridView with Combobox如何使用组合框仅填充 GridView 的选定单元格
【发布时间】:2015-09-20 13:21:25
【问题描述】:

我的目标很简单。假设我在 DataGridView 中填写了 5 行数据。在第一列中,我填写了如下所示的数据。用户应在第二列填写数据。

姓名:--

年龄:--

地区:--

出生日期:--

状态:--

现在我必须将 ComboBoxes 放置在 DataGridView 的第二列中,仅用于“District”和“State”行,并让其余行可编辑。使用 DataGridView,我可以将所有行设为 ComboBox 或 TextBox。我用谷歌搜索了这个但没有用。我也尝试了第 3 方工具,但我没有找到这种类型的实现。我尝试使用 ListView/TreeView 而不是 DataGridView,但我没有实现。我正在使用 C# Winforms VS2010。

如果有任何想法,我准备从头开始。提前致谢。

编辑: 下面是突出显示的“行”应为“组合框”而其余应为“文本框”的输出。

如果我添加以下代码,整个 Column 将更改为 ComboBox。

        DataGridViewComboBoxColumn districtColumn = new DataGridViewComboBoxColumn();
        districtColumn.DataPropertyName = "container";
        districtColumn.HeaderText = "Details";
        districtColumn.ValueType = typeof(Container);
        districtColumn.DisplayMember = "District";  
        districtColumn.ValueMember = "code";
        districtColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;

        dataGridView1.Columns.Add(districtColumn);

现在希望你清楚地了解我的问题。

谢谢..

【问题讨论】:

  • 这似乎不是DataGridView 可表示的数据。更好地查看PropertyGrid 控件。

标签: c# winforms datagridview combobox


【解决方案1】:

啊……这次我抓到你了。

你需要的是DataGridViewComboBoxCell,创建一个comboxcell并将它放在网格视图单元格中。

DataGridViewComboBoxCell  cmbcell = new DataGridViewComboBoxCell();
cmbcell.DataSource = Districts; // Your data source

DataGridViewCell normalcell = new DataGridViewTextBoxCell();
normalcell.Value = "Name"; // creating the text cell

dataGridView1.Rows[iRowIndex].Cells[1] = cmbcell; // use your own logic to set row index
dataGridView1.Rows[iRowIndex].Cells[1] = normalcell;

希望这会有所帮助!

【讨论】:

  • 我会检查这个。感谢您的建议。
  • 没问题,如果您有其他问题/说明,请在此处发布。
  • 这不起作用,因为 ComboBox 应用于列的所有行。
  • @Gopi ,我猜你正在为所有列使用组合列模板,我也更新了代码以声明其他列。
  • 是的,谢谢它工作正常。还有一个帮助,当我单击组合框项目正在显示时,但项目宽度大于组合框宽度。如何处理这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多