如何在VB.NET中实现DataGridView中TextBoxCell与ComboBoxCell的动态转换?

2026-05-06 09:341阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计655个文字,预计阅读时间需要3分钟。

如何在VB.NET中实现DataGridView中TextBoxCell与ComboBoxCell的动态转换?

我想实现一个具有两列的DataGridView。第一列将开始时为DataGridViewComboBoxColumn类型。根据该列中的选择,我希望能够将第二列中的相应单元格更改为DataGridViewComboBoxCell或DataGridViewTextBoxCell。我只需求这个。

如何在VB.NET中实现DataGridView中TextBoxCell与ComboBoxCell的动态转换?

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

我想我只需要创建DataGridViewColumn类型的第二列,但不了解如何动态更改单元格类型的机制.

我在Visual Studio 2005中使用VB.NET.

提前致谢!

更新:我认为,绕过它的一种方法是将第二列作为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翻译,我测试和工作.

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

结束班

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

乔恩

本文共计655个文字,预计阅读时间需要3分钟。

如何在VB.NET中实现DataGridView中TextBoxCell与ComboBoxCell的动态转换?

我想实现一个具有两列的DataGridView。第一列将开始时为DataGridViewComboBoxColumn类型。根据该列中的选择,我希望能够将第二列中的相应单元格更改为DataGridViewComboBoxCell或DataGridViewTextBoxCell。我只需求这个。

如何在VB.NET中实现DataGridView中TextBoxCell与ComboBoxCell的动态转换?

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

我想我只需要创建DataGridViewColumn类型的第二列,但不了解如何动态更改单元格类型的机制.

我在Visual Studio 2005中使用VB.NET.

提前致谢!

更新:我认为,绕过它的一种方法是将第二列作为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翻译,我测试和工作.

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

结束班

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

乔恩