简体   繁体   English

数据源gridview c#

[英]Datasource gridview c#

i'm new on .net, and i'm trying to make a gridview table that take data from a database (i bind data with <asp:sqldatasource selectcommand> tag) and for a specific integer value from this table column i want to display in gridview a string that is in another table and is specific for the integer.我是 .net 上的新手,我正在尝试制作一个 gridview 表,该表从数据库中获取数据(我使用 <asp:sqldatasource selectcommand> 标记绑定数据)以及我想要的该表列中的特定整数值在 gridview 中显示另一个表中特定于整数的字符串。 So 2 tables, 1 is inserted in gridview, another has static number of columns, table "a" has integers and other columns, tabel "b" has same integers but different strings on other columns for them.所以2个表,1个插入gridview,另一个有静态列数,表“a”有整数和其他列,表“b”有相同的整数,但其他列上的字符串不同。 In gridview i want to show other columns from table "a" and 1 column from table "b".在gridview中,我想显示表“a”中的其他列和表“b”中的1列。 I can display the first table but i don't have ideas to link 2 tables.我可以显示第一个表,但我没有链接 2 个表的想法。 I can't make changes in databes.我无法在数据库中进行更改。 Thank you!谢谢!

Table a                                                            Table b                      
column1 column2 column3                        column4 column5
data1    data2   integer                               integer  string

Output
Gridview
column1 column2 column5

To display the other data, then simply use a left join in your sql.要显示其他数据,只需在您的 sql 中使用左连接。

So, say we have this to load up the data grid:所以,假设我们有这个来加载数据网格:

if (IsPostBack == false)
{
    GridView1.DataSource = Myrst("Select FirstName, LastName, Hotel_ID FROM tblBooked");
    GridView1.DataBind();
}

We thus get this result:我们因此得到这个结果:

在此处输入图片说明

But, that hotel_id is rather ugly, so we want to pull that data from tblHotels但是,那个 hotel_id 相当丑陋,所以我们想从 tblHotels 中提取该数据

So, you simply left join in the other table.因此,您只需将 join 留在另一个表中。 You can write out the sql, or say lets create a view like this:你可以写出 sql,或者说让我们创建一个这样的视图:

在此处输入图片说明

Now, our simple code can say go like this:现在,我们的简单代码可以这样说:

    GridView1.DataSource = Myrst("SELECT * from vBookedHotels");
    GridView1.DataBind();

And we get this result:我们得到这个结果:

在此处输入图片说明

So the "general" approach here is to write some sql and use a left join.所以这里的“通用”方法是编写一些 sql 并使用左连接。 You can thus quite much pull in any "id" value and translate it to the other table.因此,您可以相当多地提取任何“id”值并将其转换为另一个表。 So friendly text names or descriptions can thus be pulled from the other table.因此可以从另一个表中提取友好的文本名称或描述。

I recommend using SQL for this, since then your two lines of code to load up the gridview can be done as per above.我建议为此使用 SQL,因为这样您加载 gridview 的两行代码就可以按照上述方式完成。 And it often possible that you need to do this in several places in your application - so a handy view to query against makes is rather easy.并且通常可能需要在应用程序的多个位置执行此操作 - 因此查询 make 的方便视图相当容易。

in above, I use a custom routine called MyRst(), and all it does is create the sqlcommand object, get the connection and returns a data table (i was tired of writing the same code over and over (eg: create connection, create data adaptor etc - so I just put that code in a simple routine, and now I can just type in some sql and quite much assign it to a gridview, or even a listview, or even dropdown boxes with the two lines of code as per above.在上面,我使用了一个名为 MyRst() 的自定义例程,它所做的只是创建 sqlcommand 对象,获取连接并返回一个数据表(我厌倦了一遍又一遍地编写相同的代码(例如:创建连接,创建数据适配器等 - 所以我只是把代码放在一个简单的例程中,现在我可以输入一些 sql 并将它分配给一个 gridview,甚至一个列表视图,或者甚至是带有两行代码的下拉框以上。

So, the general approach here is to use SQL to get/grab/pull and translate some "ID" in a column to some nice user friendly description or text columns in the 2nd table as you outlined.因此,这里的一般方法是使用 SQL 来获取/抓取/拉取,并将列中的一些“ID”转换为您概述的第二个表中一些很好的用户友好描述或文本列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM