DataGridViewComboBoxColumn 数据源?
我正在尝试在 DataGridView
中设置一些内容。看起来这应该很简单,但我遇到了麻烦。我想显示三列:
- CodeID
- CodeName
- ComboBox,其中 DisplayMember of TypeName、ValueMember of TypeID
我希望能够从 TypeName
的所有可能值中进行选择。这是我的困境:
如果我将所有这些加载到一个 DataTable
中并将 DataGridView
设置为 DataSource
,我可以显示现有的 该记录的 TypeName
,但组合框将不包含任何其他值。如果我将 DataGridViewComboBoxColumn
的 DataSource
设置为包含所有可能的 TypeNames
的单独 DataTable
,则现有值为不显示。
DataGridView
使用起来确实很烦人,因此无论是解决方案还是可行的替代方案都将受到赞赏。
编辑:看来问题是由于我想要为 DisplayMember
和 ValueMember
提供一个单独的项目引起的。如果我不担心将 ID
设置为 ValueMember
,则以下内容有效:
var typeColumn = new DataGridViewComboBoxColumn
{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "Type",
DataPropertyName = "Type"
}
如果我执行以下操作,则会选择正确的类型,但我无法更改组合框中的选择:
var typeColumn = new DataGridViewComboBoxColumn
{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "TypeID",
DataPropertyName = "TypeID"
}
如果我使用以下内容,则在尝试填充时会收到 FormatException
错误:
var typeColumn = new DataGridViewComboBoxColumn
{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "TypeID",
DataPropertyName = "Type"
}
edit: typeList
是一个简单的DataTable
由以下内容填充:
SELECT DISTINCT IT.InsuranceTypeID, IT.[Type]
FROM InsuranceType IT
WHERE IT.ClientID = @ClientID
ORDER BY [Type]
I'm trying to get something set up in a DataGridView
. It seems like this should be pretty straightforward but I'm having trouble. I want to display three columns:
- CodeID
- CodeName
- ComboBox with DisplayMember of TypeName, ValueMember of TypeID
I want to be able to select from all possible values of TypeName
. Here's my dilemma:
If I load all of this into one DataTable
and set the DataGridView
as the DataSource
, I can display the existing TypeName
for that record, but the combo box will not include any other values. If I set the DataSource
for the DataGridViewComboBoxColumn
to a separate DataTable
that includes all possible TypeNames
, the existing value is not displayed.
DataGridView
is really annoying to work with so either a solution for this or a viable alternative would be appreciated.
Edit: it appears the issue is caused by my wanting to have a separate item for DisplayMember
and ValueMember
. The following works, if I don't worry about setting the ID
as the ValueMember
:
var typeColumn = new DataGridViewComboBoxColumn
{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "Type",
DataPropertyName = "Type"
}
If I do the following, the right types are selected, but I can't change the selection in the combo box:
var typeColumn = new DataGridViewComboBoxColumn
{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "TypeID",
DataPropertyName = "TypeID"
}
If I use the following I get a FormatException
error as it's trying to populate:
var typeColumn = new DataGridViewComboBoxColumn
{
DataSource = typeList,
DisplayMember = "Type",
ValueMember = "TypeID",
DataPropertyName = "Type"
}
edit: typeList
is a simple DataTable
populated by the following:
SELECT DISTINCT IT.InsuranceTypeID, IT.[Type]
FROM InsuranceType IT
WHERE IT.ClientID = @ClientID
ORDER BY [Type]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个类似的(我认为)问题,我的解决方案是为
DataGridViewComboBoxColumn
设置DataSource
before 设置
。DataGridView
的数据源就我而言,我的 DataSource 分别是
List
和BindingList
但它应该与 DataTables 相同:I had a similar (I think) issue, and the solution for me was to set the
DataSource
for theDataGridViewComboBoxColumn
before setting theDataSource
for theDataGridView
.In my case, my DataSources are a
List<T>
and aBindingList<T>
respectively but it should work the same with DataTables:好的,我想出了一个示例
ClientInfo
和InsuranceDetails
我认为它们可能会模仿您想要做的事情。如果这些细节不太正确,请告诉我。此示例将填充DataGridViewComboBox
并根据InsuranceDetails
设置值(具体位于:InsurDetailz = all_insurance_types[2]
)Ok, I came up with an example
ClientInfo
andInsuranceDetails
that I think might mimic what you are trying to do. Let me know if these details arent quite right. This example will populate theDataGridViewComboBox
and set the value based on theInsuranceDetails
(specifically at:InsurDetailz = all_insurance_types[2]
)