6

目标

我的 datagridview 有两列([问题],[答案])。根据已知的问题类型是/否 复选框文本文本 、文件上传 按钮),我希望列单元格具有相应的控件

例子

数据网格视图行:

  1. [问题] 你抽烟吗?[答案](是否 复选框
  2. 【问题】你几岁?[答案](文本文本
  3. [问题] 文件上传 [答案](文件上传 按钮

工作

我以编程方式创建我的数据网格视图。

Private Sub FormatQuestionDgv(ByVal dgv As DataGridView)
    Dim ColQ As New DataGridViewTextBoxColumn
    Dim ColA As New DataGridViewColumn

    'Header text
    ColQ.HeaderText = "Question"
    ColA.HeaderText = "Answer"

    'Name
    ColQ.Name = "ColQ"
    ColA.Name = "ColA"

    'Widths
    ColQ.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
    ColA.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

    'Add columns
    With dgv.Columns
        .Add(ColQ)
        .Add(ColA)
    End With
End Sub

问题

正如您在我的工作中看到的那样,答案列是DataGridViewColumn类型。我当时不知道问题类型。因此,我将其声明为普通列,而不是DataGridViewCheckBoxColumn, DataGridViewTextBoxColumn, DataGridViewButtonColumn...

由于它们与 的类型不同DataGridViewColumn,因此出现以下错误:

错误类型错误

如何在 1 DataGridViewColumn 中添加不同的控件类型?甚至可能吗?

4

3 回答 3

9

你看过这些:

在 DataGridViewColumn 中混合单元格类型

一列的 DataGridview 单元格不能有不同的类型

http://social.msdn.microsoft.com/Forums/windows/en-US/148b232b-ce8c-4c49-b35d-50d8a5c448d1/different-cell-types-in-a-datagridview-column

继 MSDN 文章之后...

有两种方法可以做到这一点:

  1. 将 aDataGridViewCell转换为存在的特定单元格类型。例如,将 a 转换DataGridViewTextBoxCellDataGridViewComboBoxCell类型。
  2. 创建一个控件并将其添加到 的控件集合中 DataGridView,设置其位置和大小以适合要作为宿主的单元格。

下面是一些示例代码来说明这些技巧:

private void Form5_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("name");
    for (int j = 0; j < 10; j++)
    {
        dt.Rows.Add("");
    }
    this.dataGridView1.DataSource = dt;
    this.dataGridView1.Columns[0].Width = 200;

    /*
     * First method : Convert to an existed cell type such ComboBox cell,etc
     */

    DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
    ComboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" });
    this.dataGridView1[0, 0] = ComboBoxCell;
    this.dataGridView1[0, 0].Value = "bbb";

    DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
    this.dataGridView1[0, 1] = TextBoxCell;
    this.dataGridView1[0, 1].Value = "some text";

    DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();
    CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    this.dataGridView1[0, 2] = CheckBoxCell;
    this.dataGridView1[0, 2].Value = true;

    /*
     * Second method : Add control to the host in the cell
     */
    DateTimePicker dtp = new DateTimePicker();
    dtp.Value = DateTime.Now.AddDays(-10);
    //add DateTimePicker into the control collection of the DataGridView
    this.dataGridView1.Controls.Add(dtp);
    //set its location and size to fit the cell
    dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Location;
    dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Size;
}
于 2013-08-04T17:11:48.033 回答
6

其他答案非常困难且容易出错。根据要求添加所需的单元格类型更容易。

例子:

使用设计器,创建一个带有 DataGridView 和两列的表单:一列用于问题,一列用于答案。

private DataGridView dataGridView3;
private DataGridViewTextBoxColumn columnQuestion;
private DataGridViewTextBoxColumn columnAnswer;

在我的课堂上,我为答案类型创建了枚举

public enum AnswerType
{
    Text,
    YesNo,
    LoadFile,
    Combo,              
};

我创建了两种方法:一种是创建一个包含问题的单元格,另一种是为答案创建正确的单元格类型。

创建问题单元格的方法很简单:

private DataGridViewCell CreateQuestionCell(string question)
{
    return new DataGridViewTextBoxCell()
    {
        ValueType = typeof(string),
        Value = question,
        ReadOnly = true,       // questions can't be edited
    };
}

创建答案单元格的方法有一个参数,指示所需的答案类型:

private DataGridViewCell CreateAnswerCell(AnswerType answerType)
{
    // type of column depends on rowIndex
    DataGridViewCell cell;
    switch (answerType)
    {
        case AnswerType.YesNo: // Create a checkbox cell
            cell = new DataGridViewCheckBoxCell()
            {
                ValueType = typeof(bool),
                Value = false,
            };
            break;
        case AnswerType.LoadFile: // Create a Button cell
            cell = new DataGridViewButtonCell()
            {
                ValueType = typeof(string),
                Value = "Load!",
            };
            break;
        case AnswerType.Combo: // Create a Combo Cell
            var selectableValues = Enumerable.Range(0, 4);
            var comboItems = Enumerable.Range(0, 100);
            cell = new DataGridViewComboBoxCell()
            {
                DataSource = new BindingList<int>(comboItems.ToList()),
            };
            break;
        default: // Create a Text cell
            cell = new DataGridViewTextBoxCell()
            {
                ValueType = typeof(string),
                Value = "<please enter name>",
            };
            break;
    }
    return cell;
}

根据请求添加一个包含问题单元格和答案单元格的行:

private void AddRow(string question, AnswerType answerType)
{
    DataGridViewRow row = new DataGridViewRow();
    row.Cells.Add(this.CreateQuestionCell(question));
    row.Cells.Add(this.CreateAnswerCell(answerType));
    this.dataGridView1.Rows.Add(row);
}

为了测试,我创建了四个按钮和处理程序来添加行:

private Button buttonCheckbox;
private Button buttonAction;
private Button buttonCombo;
private Button buttonText;

private void OnButtonCheckbox(object sender, EventArgs e)
{
    this.AddRow("Do you smoke", AnswerType.YesNo);
}

private void OnButtonText(object sender, EventArgs e)
{
    this.AddRow("Name", AnswerType.Text);
}

private void OnButtonCombo(object sender, EventArgs e)
{
    this.AddRow("Age?", AnswerType.Combo);
}

private void OnButtonAction(object sender, EventArgs e)
{
    this.AddRow("Document upload", AnswerType.LoadFile);
}

等等,走吧!简单的来吧您好!

于 2017-03-31T07:26:14.407 回答
1
DataGridViewCellStyle styl_Column = new DataGridViewCellStyle();
         if (_myColumnCollection[i].TypeColumn == TypColumn.CheckBox)
                {
                   dtv_information.Columns.Add(chk_clmn);
                   styl_Column.NullValue = false;

                }
styl_Column.NullValue = false;
于 2014-04-04T12:49:16.260 回答