如何在DataGridViewTextBoxCell和DataGridViewComboBoxCell之间切换? [英] How to switch between DataGridViewTextBoxCell and DataGridViewComboBoxCell?

本文介绍了如何在DataGridViewTextBoxCell和DataGridViewComboBoxCell之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个DataGridView有两列。第一列将始终为DataGridViewComboBoxColumn类型。根据该列中的选择,我希望能够将第二列中的相应单元格更改为DataGridViewComboBoxCell或DataGridViewTextBoxCell。



我是认为我只需要制作DataGridViewColumn类型的第二列,但是不明白如何快速更改单元格类型的机制。



我正在工作在Visual Studio 2005中使用VB.NET。



提前感谢!



更新: strong>我想的一个方法是将第二列作为DataGridViewComboBoxColumn,并更改单元格的属性,以使其像下拉列表一样,或者作为(可编辑)下拉列表而不是元素。后者看起来像一个文本框,我可以与它一起生活,它不会涉及更改单元格的类型。

解决方案

我没有VB.Net版本,但希望这个快速的C#代码片段可以帮助您或指向正确的方向。



在这个例子中,我设置了一个带有2列的简单DataGridView。第一个是DataGridViewComboBox,它包含两个选项:Text或Combo。



第二列最初设计为设计器的DataGridViewTextBoxColumn。



我处理DataGridView上的CurrentCellDirtyStateChanged事件。我检查单元格是否脏,只检查第一列(ComboBox)。您必须调用CommitEdit来获取组合中的新值,否则您将查看以前的值。根据组合框中的选择,然后使用该类型的新单元格覆盖第二列中的单元格。



您将添加自己的逻辑下降并处理该值)。您可能想要存储该值,然后将其放回单元格或其他任何内容。



这是我使用的代码,并进行了一个快速而肮脏的测试:

  private void dataGridView1_CurrentCellDirtyStateChanged(object sender,EventArgs e)
{
if(dataGridView1.IsCurrentCellDirty == false)
{
return;
}

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

if(dataGridView1.CurrentCell.ColumnIndex == 0)
{
if(((string)dataGridView1.CurrentCell.Value)==Text)
{
dataGridView1.Rows [dataGridView1.CurrentCell.RowIndex] .Cells [1] = new DataGridViewTextBoxCell();
}
else if(((string)dataGridView1.CurrentCell.Value)==Combo)
{
dataGridView1.Rows [dataGridView1.CurrentCell.RowIndex] .Cells [ 1] = new DataGridViewComboBoxCell();
}
}
}

这是一个快速的VB翻译,我测试和工作。

 公共类Form1 

私有子DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As System .Object,ByVal e As System.EventArgs)处理DataGridView1.CurrentCellDirtyStateChanged

如果DataGridView1.IsCurrentCellDirty = False然后
返回
结束如果

DataGridView1。 CommitEdit(DataGridViewDataErrorContexts.Commit)

如果DataGridView1.CurrentCell.ColumnIndex = 0然后

如果CStr(DataGridView1.CurrentCell.Value)=Text然后
DataGridView1 .Rows(DataGridView1.CurrentCell.RowIndex).Cells(1)= New DataGridViewTextBoxCell

ElseIf CStr(DataGridView1.CurrentCell.Value)=Combo然后
DataGridView1.Rows(DataGridView1.CurrentCell .RowIndex).Cells(1)= New DataGridViewComboBoxCell
End If

如果


End Sub

结束类



您将丢失该列中存储的任何值,因此您需要先保存。



Jon


I want to have a DataGridView that has two columns. The first column will always be of type DataGridViewComboBoxColumn. Based on the selection in that column, I'd like to be able to change the corresponding cell in the second column to either a DataGridViewComboBoxCell or a DataGridViewTextBoxCell.

I'm thinking I just need to make the second column of type DataGridViewColumn, but don't understand the mechanics of how to change the cell type on the fly.

I'm working with VB.NET in Visual Studio 2005.

Thanks in advance!

Update: One way around it, I suppose, is to make the second column as a DataGridViewComboBoxColumn, and change the attributes of the cell so that it either behaves like a drop-down list, or as an (editable) drop-down with no elements. The latter looks enough like a text box that I could live with it, and it wouldn't involve changing the type of the cell.

解决方案

I don't have the VB.Net version,but hopefully this quick C# snippet will help you or point you in the right direction.

In this example, I set up a simple DataGridView with 2 columns. The first being a DataGridViewComboBox populated with two choices: "Text" or "Combo".

The second column is initially set to DataGridViewTextBoxColumn from the designer.

I handle the CurrentCellDirtyStateChanged event on the DataGridView. I check if the cell is dirty and only check the first column (The ComboBox). You gotta call the CommitEdit to get the new value in the combo or else you will be looking at the previous value. Based on the selection in the combo box I then overwrite the cell in the 2nd column with a new cell of that type.

You would add your own logic (populate the drop downs and handle the value). You might want to store the value and then put it back into the cell or whatever.

Here is the code I used and did a quick and dirty test on:

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (dataGridView1.IsCurrentCellDirty == false)
        {
            return;
        }

        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

        if (dataGridView1.CurrentCell.ColumnIndex == 0)
        {               
            if (((string)dataGridView1.CurrentCell.Value) == "Text")
            {
                dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1] = new DataGridViewTextBoxCell();
            }
            else if (((string)dataGridView1.CurrentCell.Value) == "Combo")
            {
                dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1] = new DataGridViewComboBoxCell();
            }
        }
    }

Here is a quick VB translation, that I tested and works.

Public Class Form1

Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged

    If DataGridView1.IsCurrentCellDirty = False Then
        Return
    End If

    DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)

    If DataGridView1.CurrentCell.ColumnIndex = 0 Then

        If CStr(DataGridView1.CurrentCell.Value) = "Text" Then
            DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(1) = New DataGridViewTextBoxCell

        ElseIf CStr(DataGridView1.CurrentCell.Value) = "Combo" Then
            DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(1) = New DataGridViewComboBoxCell
        End If

    End If


End Sub

End Class

You will lose any value stored in that column, so you would need to save it first.

Jon

这篇关于如何在DataGridViewTextBoxCell和DataGridViewComboBoxCell之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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