Click here to Skip to main content
Licence 
First Posted 2 Aug 2004
Views 63,066
Bookmarked 14 times

This is a simple C# program that illustrate the usage of DataGrid with DataSet

By | 2 Aug 2004 | Article
This is a simple C# program that illustrates the usage of DataGrid with DataSet.

Introduction

This is a simple C# Program that illustrate the usage of DataGrid with DataSet.

Create Database and Table accordingly.

/* 
 * Simple C# example to illustrate the usage of Dataset with DataGrid 
 * Need to change the url and query accordingly 
*/ 
  
using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Windows.Forms; 

public class DataGridSample:Form{ 
        DataGrid myGrid; 
   
        SqlConnection con; 
        SqlDataAdapter adapter; 
        DataSet ds; 
    Button ok, cancel; 
        
        SqlParameter workParam = null; 
        
        // apply to the columns in the table 
        string query = "select CardNo,CardType,CardAmount, CardHolderName from CardTest"; 
        
        // change the Server ,uid, pwd and database accordingly 
        string url = "server=TR4;uid=sa;pwd= ;database=RBSGTest"; 



        static void Main(){ 
        Application.Run(new DataGridSample()); 
        } 

        public DataGridSample(){ 
        InitializeComponent(); 
        } 

        public void InitializeComponent(){ 
        this.ClientSize = new System.Drawing.Size(550, 450); 
        myGrid = new DataGrid(); 
        myGrid.Location = new Point (10,10); 
        myGrid.Size = new Size(500, 350); 
        this.Text = "C# DataGrid with DataSet - Example"; 
        this.Controls.Add(myGrid); 
        
                ok = new Button(); 
                ok.Location = new Point(10, 375); 
        ok.Size = new Size(70, 30); 
                ok.TabIndex = 1; 
                ok.Text = "OK"; 
                this.Controls.Add(ok);  
                ok.Click += new System.EventHandler(button_Click); 

                cancel = new Button(); 
                cancel.Location = new Point(95, 375); 
            cancel.Size = new Size(70, 30); 
                cancel.TabIndex = 1; 
                cancel.Text = "Cancel"; 
                this.Controls.Add(cancel);              
                cancel.Click += new System.EventHandler(button_Click); 

        ConnectToData(); // establish database connection and create DataSet 
        myGrid.SetDataBinding(ds, "CardTest"); 
        DataTable t = ds.Tables["CardTest"]; 
        t.RowChanged += new DataRowChangeEventHandler(Row_Changed); 
   
        } 
        
        public void ConnectToData(){ 
                ds = new DataSet(); 
            con = new SqlConnection(url); 
            adapter = new SqlDataAdapter(); 
            adapter.SelectCommand = new SqlCommand(query, con); 
            adapter.Fill(ds, "CardTest"); 
                insertCommand(); 
                updateCommand(); 
        } 
        
                public void updateCommand() 
                {       
                string query = "Update CardTest Set CardHolderName = @CardHolderName, CardType = @CardType, CardAmount = @CardAmount WHERE CardNo = @CardNo";

                adapter.UpdateCommand = new SqlCommand(query, con); 
        
            workParam = adapter.UpdateCommand.Parameters.Add("@CardNo", SqlDbType.NChar); 
            workParam.SourceColumn = "CardNo"; 
            workParam.SourceVersion = DataRowVersion.Original; 
        
            workParam = adapter.UpdateCommand.Parameters.Add("@CardType", SqlDbType.NChar, 50); 
            workParam.SourceVersion = DataRowVersion.Current; 
            workParam.SourceColumn = "CardType"; 

            workParam = adapter.UpdateCommand.Parameters.Add("@CardAmount", SqlDbType.Int); 
            workParam.SourceColumn = "CardAmount"; 
            workParam.SourceVersion = DataRowVersion.Current; 

            workParam = adapter.UpdateCommand.Parameters.Add("@CardHolderName", SqlDbType.NChar, 50); 
            workParam.SourceColumn = "CardHolderName"; 
            workParam.SourceVersion = DataRowVersion.Current; 
                } 

        
          public void button_Click(object sender, EventArgs evArgs) 
          { 
                if (sender==ok){ 
                        UpdateValue(); // update the database once everything done. 
                } 
                if (sender==cancel) { 
                        this.Dispose(); 
                } 
        } 

        
        private void Row_Changed(object ob, DataRowChangeEventArgs e) 
          { 
                DataTable t = (DataTable)  ob; 
                Console.WriteLine("RowChanged " + e.Action.ToString() + "\t" + e.Row.ItemArray[0]); 
          } 
          
                public void insertCommand() 
        {       
                string insertQuery = "Insert into CardTest VALUES (@CardNo, @CardType, @CardAmount, @CardHolderName)"; 
                adapter.InsertCommand = new SqlCommand(insertQuery, con); 
        
            workParam = adapter.InsertCommand.Parameters.Add("@CardNo", SqlDbType.NChar); 
            workParam.SourceColumn = "CardNo"; 
            workParam.SourceVersion = DataRowVersion.Current; 
        
            workParam = adapter.InsertCommand.Parameters.Add("@CardType", SqlDbType.NChar, 50); 
            workParam.SourceVersion = DataRowVersion.Current; 
            workParam.SourceColumn = "CardType"; 

            workParam = adapter.InsertCommand.Parameters.Add("@CardAmount", SqlDbType.Int); 
            workParam.SourceColumn = "CardAmount"; 
            workParam.SourceVersion = DataRowVersion.Current; 
        
            workParam = adapter.InsertCommand.Parameters.Add("@CardHolderName", SqlDbType.NChar, 50); 
            workParam.SourceVersion = DataRowVersion.Current; 
            workParam.SourceColumn = "CardHolderName"; 
        } 
        
        
        public void UpdateValue() 
        { 
            try 
            { 
              adapter.Update(ds, "CardTest"); 
              Console.Write("Updating DataSet succeeded!"); 
            } 
            catch(Exception e) 
            { 
              Console.Write(e.ToString()); 
            } 
        } 
          
} 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

mubbsher

Web Developer

United Arab Emirates United Arab Emirates

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionTrying this in MySQL? Pinmemberbenjiwa18:53 1 Nov '06  
GeneralIn case this shall be an introductory article PinmemberVanarion0:44 12 Aug '04  
QuestionHow to respond to grid-row change? PinmemberPaul Ritchie16:11 11 Aug '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 3 Aug 2004
Article Copyright 2004 by mubbsher
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid