954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Two Tables In DataGridView

Hi all i want to display two unrelated tables in the same datagridview. I m using merge method of datasets but in vain. Any idea Plz

Gaurav arora
Junior Poster in Training
59 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Dare I ask why? Why not use 2 different views next to each other?

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Actually I want it to be done throuth DataSets. Actually it is an assignment

Gaurav arora
Junior Poster in Training
59 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

I meant dataviews as in table on the form, I was expecting it to be 2 datasets.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Yes LIZr I have 2 DataSets And i Have Merged Them through Merge method of datasets. But Still I m not getting my result
I have Tried following
ds1.Merge(ds2);
dataGridView1.DataSource = ds1.Tables[0].DefaultView;

Any idea plz

Gaurav arora
Junior Poster in Training
59 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

K, but merge probably doesnt do what you're expecting.

Im guessing if you had tables such as

Table 1
--------

id    value
1     5
2     10

and

table 2 
--------
uid name
3    john
4    sarah


Im guessing you wanted

id value uid name
1  5       3   john
2  10     4   sarah


yes?

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Hi,
try this

ds1.Merge(ds2);
dataGridView1.DataSource = ds1.Tables[0];
dataGridView1.DataBind();

reena12
Light Poster
32 posts since Oct 2008
Reputation Points: 12
Solved Threads: 4
 

SqlConnection cn = new SqlConnection();

string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Resources\DBMYC.mdf;Integrated Security=True;User Instance=True";
SqlConnection con = new SqlConnection(connstr); //SqlCommand cmd = sqlConnection1.CreateCommand();

SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text; //this has the option of caling a stored procedure
cmd.CommandText = "THE SQL QUERY FOR THE SELECTION FROM TWO TABLES OR MORE";

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;

//DataSet ds = new DataSet();
//da.Fill(ds, "MYCTAB");

DataTable DT = new DataTable(); //USES DATATABLE INSTEAD OF DATASET
DT.Locale = System.Globalization.CultureInfo.InvariantCulture;
da.Fill(DT);

dataGridView2.DataSource = DT;

THIS WOULD BE OF HELP, IT WORKS.

SUNNY.

SUNFARMS
Newbie Poster
1 post since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You