如何在DataGridView中创建Lookup字段? [英] How to create LookUp fields in DataGridView?

查看:99
本文介绍了如何在DataGridView中创建Lookup字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 DataGridView 中,我正在显示一个表中的许多列。在此表中,我有一列指向另一个表中的项目。您可能已经猜到了,我想在网格的第一列中显示第二个表中的某些文本值,而不是和ItemID。
我在网上找不到合适的示例。

In my DataGridView I'am displaying a buch of columns from one table. In this table I have a column which points to item in another table. As you may already guessed, I want to display in the grid in one column some text value from the second table instead of and ItemID. I could not find a right example on the net how to do this.

让我们假设我在数据库中有两个表:

Lets assume that I have two tables in databes:

表用户:

UserID UserName UserWorkplaceID
  1     Martin        1
  2     John          1
  3     Susannah      2
  4     Jack          3

餐桌上的工作场所:

WorkplaceID WorkplaceName
     1        "Factory"
     2        "Grocery"
     3        "Airport"

我有一个无类型数据集 dsUsers ,一个绑定源 bsUsers 和两个 DataAdapters 来填充数据集( daUsers daWorkplaces )。

I have one untyped dataset dsUsers, one binding source bsUsers, and two DataAdapters for filling dataset (daUsers, daWorkplaces).

我正在执行的代码:

daUsers.Fill(dsUsers);
daWorkplaces.Fill(dsUsers);
bsUsers.DataSource = dsUsers.Tables[0];
dgvUsers.DataSource = bsUsers;

这时我在 dgvUsers 中看到了三列,即UserID,UserName和UserWorkplaceID。但是,我想看到的是 Factory, Grocery等等,而不是UserWorkplaceID和值1,2,3 ...

At this point I see in my dgvUsers three columns, UserID, UserName and UserWorkplaceID. However, instead of UserWorkplaceID and values 1,2,3 I would like to see "Factory", "Grocery" and so on...

所以我添加了 dgvUsers 的另一列称为 WorkplaceName,在我的代码中,我试图将其绑定到新创建的关系:

So I've added another column to dgvUsers called "WorkplaceName" and in my code I am trying to bind it to the newly created relation:

dsUsers.Relations.Add("UsersWorkplaces", dsUsers.Tables[1].Columns["WorkplaceID"], dsUsers.Tables[0].Columns["UserWorkplaceID"]);

WorkplaceName.DataPropertyName = "UsersWorkplaces.WorkplaceName";

不幸的是,这不起作用。关系已创建,没有错误,但运行程序后此列中的字段为空。

Unfortunately that doesn't work. Relation is created without errors but fields in this column are empty after running the program.

我在做什么错了?

我还想问一个有关DataGridView中LookUp组合框的示例,该示例允许我更改UserWorkplaceID,但不是数字值,而是显示WorkplaceName下的tex值。

I would like to also ask about an example with LookUp combobox in DataGridView which allow me to change the UserWorkplaceID but instead of numeric value it will show a tex value which is under WorkplaceName.

谢谢您的时间。

推荐答案

我不知道您是否可以完全按照自己的意愿去做,哪个似乎是同时将DataGridView绑定到两个不同的 DataTable 实例。我不认为 DataGridView 类支持该功能-否则是否是我从未见过的忍者式动作。

I don't know if you can do exactly what you want, which seems to be binding the DataGridView to two different DataTable instances simulataneously. I don't think the DataGridView class supports that -- or if it does it's a ninja-style move I haven't seen.

根据MSDN ,您最好的选择可能是使用DataGridView上的 CellFormatting 事件,并检查正在格式化的单元格何时在查找列中,然后可以用其他表中的值代替。使用一个未绑定的列作为WorkplaceName列,隐藏UserWorkplaceID列,然后实现CellFormatting事件句柄以在行中查找值,例如:

Per MSDN, your best bet is probably using the CellFormatting event on the DataGridView and check for when the cell being formatted is in the lookup column, then you could substitute your value from the other table. Use an unbound column for the WorkplaceName column, hide the UserWorkplaceID column and then implement the CellFormatting event handle to look up the value in the row, e.g.:

private void dgv_CellFormatting(object sender, 
    DataGridViewCellFormattingEventArgs e)
{
    if (dgv.Columns[e.ColumnIndex].Name.Equals("WorkplaceName")
    {
        // Use helper method to get the string from lookup table
        e.Value = GetWorkplaceNameLookupValue(
            dataGridViewScanDetails.Rows[e.RowIndex].Cells["UserWorkplaceID"].Value);
    }
}

如果有很多行可见,这可能会影响性能,但可能是使其正常工作的一种不错的方法。

If you've got a lot of rows visible, this might impact performance but is probably a decent way to get it working.

如果这对您没有吸引力,则可以使用 DataTable.Merge() 合并查询的方法能够进入您的主表。快速浏览一下我的一本ADO.NET书籍,尽管我没有尝试过,但它应该可以工作。但是我不确定这是否与您先前击落的建议过于接近。

If this doesn't appeal to you, maybe use the DataTable.Merge() method to merge your lookup table into your main table. A quick glance at one of my ADO.NET books suggests this should work, although I have not tried it. But I'm not sure if this is too close to the idea suggested previously which you shot down.

关于查找组合框的第二个问题,您应该将其真正发布在一个单独的问题中,以便引起适当的注意。

这篇关于如何在DataGridView中创建Lookup字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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