ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

adplus-dvertising
Merge Case 4: Common Column, with Primary Key
Previous Home Next
  1. In the strongly typed DataSet, Table1 can be used as a sample table that has primary keys defined on it.
  2. create the column with primary key
    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("Sno", typeof(Int32));
    stb.Columns.Add("Smark", typeof(String));
    
  3. 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);
    stb.Columns.Add("Sno", typeof(Int32));
    stb.Columns.Add("Smark", typeof(Int32));
    stb.LoadDataRow(new object[] { "1", "480" }, true);
    stb.LoadDataRow(new object[] { "2", "495" }, true);
    dataGridView1.DataSource = ftb;
    dataGridView2.DataSource = stb;  
    }
    private void button1_Click(object sender, EventArgs e)
    {
    ftb.Merge(stb);
    dataGridView1.DataSource=ftb;
    dataGridView2.Visible = false; 
    label1.Text = "After Merging";
    label2.Visible = false;
    }
    }
    }
    
  4. Run the program
  5. click the merge button
Previous Home Next