DataBinding与DataGridView C# [英] DataBinding with a DataGridView C#

查看:576
本文介绍了DataBinding与DataGridView C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库表,我想显示在一个DataGridView。但是,它有几个外键,我不想向用户显示表示其他表的整数。



我有这个DataTable的列userId
我有另一个DataTable的列id和列用户名



我希望DataGridView显示用户名,而不是userId,其中userId = id。 p>

我该如何做?我还必须在同一个表中的几个列上执行此操作才能显示。



编辑:窗体而不是网络表单。

解决方案

如何填充DataTable?您可以使用连接和适当的列指定自定义SQL到DataAdapter的.SelectCommand,或在xxxCommand对象的.CommandText中指定自定义SQL。



p>

  myAdapter.SelectCommand =select a.column1,a.column2,b.username from tablea a inner join tableb b on b.userid = a.userId

  myCommand.CommandType = Text; 
myCommand.CommandText =select a.column1,a.column2,b.username from tablea a inner join tableb b on b.userid = a.userId;

或者如前所述,您可以在数据库中创建一个视图或存储过程,并调用它。


I have a Database table that I want to display in a DataGridView. However, it has several foreign keys and I don't want to show the integer that represents the other Table to the user.

I have this DataTable with a column userId I have another DataTable with a column id, and a column username

I want the DataGridView to show the username instead of userId where the userId = id.

How can I do this? I also have to do this on several columns in the same table to be displayed.

edit: windows forms, not web forms.

解决方案

How are you populating the DataTable? You can specify custom SQL with the joins and appropriate columns to the .SelectCommand of a DataAdapter or specify the custom SQL in the .CommandText of a xxxCommand object.

Eg

myAdapter.SelectCommand = "Select a.column1, a.column2, b.username from tablea a inner join tableb b on b.userid = a.userId"

Or

myCommand.CommandType = Text;
myCommand.CommandText = ""Select a.column1, a.column2, b.username from tablea a inner join tableb b on b.userid = a.userId";

Or as has been mentioned, you could create a view or storedproc in your database and call that.

这篇关于DataBinding与DataGridView C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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