469,270 Members | 1,720 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

How to Merge 2 random datatables into one and display it in gridview

Hi friends i can any one help me merging 2 random tables into one and displaying in gridview
for example
table 1
Sid SName Dno

and
table 2
Dno Dname Daddress


i want to Merge & display these 2 tables as one table in datagridview as following


Sid SName Dno Dname Daddress

please Help me

the program which i wrote till is like this
try to change it to get out put


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace DatabAse
  10. {
  11.     public partial class Form2 : Form
  12.     {
  13.         DataTable dt1, dt2;
  14.         DataSet ds;
  15.         DataRow r;
  16.         DataColumn c1, c2, c3, c4, c5, c6;
  17.         public Form2()
  18.         {
  19.             InitializeComponent();
  20.             ds = new DataSet();
  21.  
  22.         }
  23.  
  24.         private void button1_Click(object sender, EventArgs e)
  25.         {
  26.             r = dt1.NewRow();
  27.             r[0] = textBox4.Text;
  28.             r[1] = textBox5.Text;
  29.             r[2] = textBox6.Text;
  30.             dt1.Rows.Add(r);
  31.             MessageBox.Show("Row added ");
  32.         }
  33.  
  34.         private void button5_Click(object sender, EventArgs e)
  35.         {
  36.             dt1 = new DataTable();
  37.             dt1.TableName = "Table1";
  38.             dt2 = new DataTable();
  39.             dt2.TableName = "Table2";
  40.  
  41.             c1 = new DataColumn(textBox1.Text);
  42.             c2 = new DataColumn(textBox2.Text);
  43.             c3 = new DataColumn(textBox3.Text);
  44.  
  45.             c4 = new DataColumn(textBox10.Text);
  46.             c5 = new DataColumn(textBox11.Text);
  47.             c6 = new DataColumn(textBox12.Text);
  48.  
  49.             dt1.Columns.Add(c1); dt1.Columns.Add(c2); dt1.Columns.Add(c3);
  50.             dt2.Columns.Add(c4); dt2.Columns.Add(c5); dt2.Columns.Add(c6);
  51.             //dt1.PrimaryKey=c3;
  52.             //dt2.PrimaryKey=c4;
  53.             ds.Tables.Add(dt1);
  54.             ds.Tables.Add(dt2);
  55.  
  56.             MessageBox.Show("Table's created ");
  57.         }
  58.  
  59.         private void button2_Click(object sender, EventArgs e)
  60.         {
  61.             r = dt2.NewRow();
  62.             r[0] = textBox7.Text;
  63.             r[1] = textBox8.Text;
  64.             r[2] = textBox9.Text;
  65.             dt2.Rows.Add(r);
  66.             MessageBox.Show("Row added ");
  67.         }
  68.  
  69.         private void button3_Click(object sender, EventArgs e)
  70.         {
  71.             dataGridView1.DataSource = dt1;
  72.             dataGridView2.DataSource = dt2;
  73.         }
  74. }
  75.  

if any one has output please tell me
Thanks in advance
geels
Oct 2 '09 #1
14 8242
Frinavale
9,735 Expert Mod 8TB
What are you trying to do?

Your code doesn't make sense to me. I can't imagine it working at all.

Please take a look a the API for the DataTable class.

Please take a look at DataTable.Merge Method as well.

Please keep in mind that your Tables are going to be destroyed after every page request has finished processing. This means that the Tables won't be available to the next page request unless you either recreate them or retrieve them from some sort of cache store.
Oct 2 '09 #2
this code is actually for windows programming
button5_Click is code for creating 2 Data Tables Getting input from TextBoxes to Set column names Dynamically


button1_Click is for adding Data to the First table from text boxes as input

button2_Click is for adding data to second table from text boixes as input

button3_Click is for binding those data to DATA GRID VIEW

okayif its so confusing
can you please write fresh programe for getting Out put
Oct 2 '09 #3
Frinavale
9,735 Expert Mod 8TB
Are you developing an ASP.NET application (that uses a GridView)

Or

Are you developing a desktop application (that should be using a DataGridView)

???
Oct 2 '09 #4
iam developing windows application
(desktop application ) i need DataGridView
Oct 2 '09 #5
Frinavale
9,735 Expert Mod 8TB
Well that explains why I was so confused.

GridViews are used in ASP.NET web applications. They work differently than DataGridViews that are used in desktop applications.

Did you take a look at the DataTable Merge() method that I linked you to?
Oct 2 '09 #6
Yah i looked
but problem is still there while merging two tables
it is displaying output some how like this

<br>
Sid SName Dno Dname Daddress
<br>
1 sam 1 <br>
2 ram 1 <br>
<br>
3 amir 2 <br>
4 rama 1 <br>
. 1 IT hyd <br>
. 1 IT Banglore<br>
. 2 IT hyd

i want out put as really merged
Oct 2 '09 #7
Frinavale
9,735 Expert Mod 8TB
Are the "<br>" HTML tags in there??
Oct 2 '09 #8
Out put using merge is like this
Expand|Select|Wrap|Line Numbers
  1. Sid   SName      Dno      Dname     Daddress
  2. 1      sam         1 
  3. 2      ram         1 
  4. 3      amir        2
  5. 4      rama        1
  6. .                  1      IT        hyd
  7. .                  1      IT        Banglore
  8. .                  2      IT        hyd 
but i want it to be like this
Expand|Select|Wrap|Line Numbers
  1. Sid      SName      Dno      Dname      Daddress
  2. 1        sam        1        IT         hyd
  3. 2        ram        1        IT         Banglore
  4. 3        amir       2        IT         hyd
  5. 4        rama       1        IT         hyd
  6. .
Oct 2 '09 #9
no <br> is there actually i was thinking this post is HTml so used it
Oct 2 '09 #10
Frinavale
9,735 Expert Mod 8TB
Ok don't add <br> tags because they will just appear as text (html code provided will be rendered as pure text to protect users like you in case someone decide to provide harmful <script>s)

Please use code tags instead. Even for trying to display table data...use spaces instead of tabs when posting table stuff and it will be much more readable.


I'm sorry but what's the difference between the two outputs?
Oct 2 '09 #11
First table in above is having
for first 4 records theer is Gap for Dname Daddress

and remaining next records there is Gap for
Sid SName

i want them to be mearged as one unit
Oct 2 '09 #12
Frinavale
9,735 Expert Mod 8TB
Oh...
You don't want to merge tables. You want to Join the tables.

Are you retrieving your data from a database? If so, modify your SQL to Join the tables so that you don't have to do this in your C# code.

-Frinny
Oct 2 '09 #13
im not using any database im just generating random database with in Dataset
so i cant use SQL

its actually logical one not related with database

-- santosh
Oct 2 '09 #14
Frinavale
9,735 Expert Mod 8TB
This MSDN article called Join Method-Based Query Syntax Examples (LINQ to DataSet) looks like it will join two tables in a DataSet...the problem is that the resultant isn't a DataTable.

I think you should still be able to bind your DataGridView to the resulting collection though...

If LINQ doesn't work for you then I think you're going to have to write a method that does the table Join.

-Frinny
Oct 2 '09 #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by ralphedge | last post: by
reply views Thread by jencus | last post: by
3 posts views Thread by Michel Esber | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.