Hi

I crated a datagrid view that gets the data from the database. Now i need to get data from 2 tables which have a relationship

Item and Item Type


because i want to show the items grouoped by the item type.. Is it possible?

So you have data in 2 DataTables? One is called Item, 2nd one is called ItemType.
Can you explain ones more a bit more clearly which data would you like to show in dgv?

So you have data in 2 DataTables? One is called Item, 2nd one is called ItemType.
Can you explain ones more a bit more clearly which data would you like to show in dgv?

I am sorry for my bad english!

I have two tables
-> Item
-> ItemType

These two tables have a relationship between them. Now I want to show The Item Name and Type in one datagridview. Is this possible? and if so how can i do it?

I do not have any code i just have tables in a database

tblItem
-> ID
-> Name
-> TypeID (F.K.)
-> Price


tblItemType
-> ID (P.k.)
-> Type

And you would like to get which columns, and show them in dgv?
Use can fill DataTable with SELECT query, and bind dataTable to the dgv:

DataTable table = new DataTable();
//use sqlConnection and SqlCommand to create the command with this query:
sting query = @"SELECT tblItem.Id, tblItem.Name, tblItem.Price, tblItemType.Type FROM tblItem, tblItemType WHERE tblItemType.ID = tblItem.TypeID";
SqlConnection sqlConn = new SqlConnection("connString");
SqlCommand cmd =new SqlCommand(query, sqlConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(table);
//bind data from dataTable to the dgv:
this.dataGridView1.DataSource = table;

This is it.

Edited 5 Years Ago by Mitja Bonca: n/a

Was the post in any help? Iam not sure, because I was guessing what do you want to have. I was only assming, out of the table names you have me.

Display of data in datagridview basis on what query you write not on the number of tables.. You can join two tables if you want to add values from both tables

And if for a datagrid view, first u retrieve the values of one data table and store them in Dataset, then after that using this code

Dataset ds= new Dataset();
ds.Columns.add(<newcolumnname>);
ds.Tables(0).Rows(0)(<columnNo>) = value(which u get from another table)

after filling all the columns required in to dataset using

datagrid.datasource()=ds;
datagrid.databind();

you can fill in the gridview what ever values you want.

This article has been dead for over six months. Start a new discussion instead.