ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

Merge Case 5: Absolutely Different Table Structures
Previous Home Next
  1. In this case, the two table structures have nothing in common. All the columns differ
  2. create the different column
    DataTable ftb = new DataTable();
    DataTable stb = new DataTable();
    ftb.Columns.Add("Sno", typeof(Int32));
    ftb.Columns.Add("Sname", typeof(String));
    ftb.PrimaryKey = new DataColumn[] { ftb.Columns["Sno"] };
    stb.Columns.Add("Smark", typeof(Int32));
    stb.Columns.Add("Sgrade", typeof(String));
    
  3.  

  4. write the code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;
    namespace WindowsFormsApplication15
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    DataTable ftb = new DataTable();
    DataTable stb = new DataTable();
    private void Form1_Load(object sender, EventArgs e)
    {
    ftb.Columns.Add("Sno", typeof(Int32));
    ftb.Columns.Add("Sname", typeof(String));
    // ftb.PrimaryKey = new DataColumn[] { ftb.Columns["Sno"] };
    ftb.LoadDataRow(new object[] { "1", "Ashish" }, true);
    ftb.LoadDataRow(new object[] { "2", "KAMAL" }, true);
    ftb.LoadDataRow(new object[] { "3", "Mohit" }, true);
    ftb.LoadDataRow(new object[] { "4", "Aditya" }, true);
    stb.Columns.Add("Smark", typeof(Int32));
    stb.Columns.Add("Sgrade", typeof(string));
    stb.LoadDataRow(new object[] { "480", "I" }, true);
    stb.LoadDataRow(new object[] { "255", "II" }, true);
    stb.LoadDataRow(new object[] { "295", "II" }, true);
    stb.LoadDataRow(new object[] { "380", "I" }, true);
    dataGridView1.DataSource = ftb;
    dataGridView2.DataSource = stb;  
    }
    private void button1_Click(object sender, EventArgs e)
    {
    ftb.Merge(stb);
    dataGridView3.DataSource=ftb;
    dataGridView1.Visible = false;
    dataGridView2.Visible = false;
    dataGridView3.Visible =true;  
    label1.Text = "After Merging";
    label2.Visible = false;
    }
    }
    }
    
  5. Run the program
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: