将 ComboBox 添加到特定行的 datagridview [英] Adding ComboBox to a datagridview for a particular row

查看:26
本文介绍了将 ComboBox 添加到特定行的 datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将下拉列表显示到几行中的某一行.我的 dataGridView 有 2 列(参数和值),我在代码中动态添加了 3 行.对于所有 3 行,参数文本是固定的,用户不能修改.对于第一行,参数文本是prm1",值文本将从下拉列表中选择.其他 2 行,值文本将是用户输入的文本框.我尝试搜索但找不到答案.请在这里帮助我.

I have a requirement to show drop down list to one particular row among several rows. My dataGridView has 2 columns (Parameter and Value) and I am adding 3 rows dynamically in code. For all 3 rows, paramter texts are fixed and can't be modified by user. For first row, Parameter text is "prm1" and Value text will be chosen from drop down list. other 2 rows, Value text will be text box entered by User. I tried searching but couldn't find the answer. Please help me here.

推荐答案

可以在运行时替换特定的 DataGridView 单元格 - 例如,在您想要在一个单元格中使用组合框的情况下,您可以拥有一个 DataGridViewTextBoxColumn 并替换第一行的单元格.像这样:

It is possible to replace specific DataGridView cells at run time - in your situation for example where you want a combobox in one cell you can have a DataGridViewTextBoxColumn and replace the cell in the first row. Something like this:

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    List<Book> books = new List<Book>();
    books.Add(new Book { bookID = 1, bookName = "Test-Driven Development (Kent Beck)" });
    books.Add(new Book { bookID = 2, bookName = "Refactoring (Martin Fowler)" });
    books.Add(new Book { bookID = 3, bookName = "Code Complete: 2nd Edition (Steve McConnell)" });
    DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();

    c.DataSource = books;
    c.Value = 1;
    c.ValueMember = "bookID";
    c.DisplayMember = "bookName";

    dataGridView1.Rows[0].Cells[0] = c;
}

您也可以通过另一种方式执行此操作,将 DataGridViewComboBoxColumn 中的特定组合框单元格替换为 DataGridViewTextBoxCell.

You can also do this the other way and replace a particular combobox cell from a DataGridViewComboBoxColumn with a DataGridViewTextBoxCell.

有一点 - 虽然这会起作用,但更好的可用性可能只是将一些组合框设置为只读.

One thing though - while this will work, better usability might just come from setting some combo boxes to read only.

这篇关于将 ComboBox 添加到特定行的 datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆