简体   繁体   中英

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. 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. In gridview i want to show other columns from table "a" and 1 column from table "b". I can display the first table but i don't have ideas to link 2 tables. 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.

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

So, you simply left join in the other table. You can write out the sql, or say lets create a view like this:

在此处输入图片说明

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. You can thus quite much pull in any "id" value and translate it to the other table. 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. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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