Click here to Skip to main content
Licence 
First Posted 10 Aug 2004
Views 40,200
Bookmarked 17 times

How to create a simple Master-Slave DataGrid

By | 10 Aug 2004 | Article
This article shows how to create a simple Master-Slave DataGrid.

Introduction

In this article, I will provide code sample on how to make a simple Master-Slave DataGrid. First, add a DataGrid on the page and make a "Select" column using property builder.

Sample screenshot

Sample screenshot

Master-Slave DataGrid in C# Code

private void Page_Load(object sender, System.EventArgs e)
{
  if(!Page.IsPostBack) 
  { 
    // Binds the DataGrid
    BindGrid(); 

    }
  } 
  public void BindGrid() 
  { 
    SqlCommand myCommand = 
        new SqlCommand("SELECT * FROM Categories",myConnection); 
    myCommand.CommandType = CommandType.Text; 
    try 
    { 
      // Opens database connection
      myConnection.Open(); 
      // Execute SqlCommand that returns datareader
      SqlDataReader dr = 
          myCommand.ExecuteReader(CommandBehavior.CloseConnection);
      // Populate MasterGrid
      MasterGrid.DataSource = dr; 
      // Binds MasterGrid to the page
      MasterGrid.DataBind(); 

    } 

    // Catch SqlException 
    catch ( SqlException SqlEx ) 
    { 
      // TODO
    }
    // Catch general Exception
    catch ( Exception ex ) 
    {
      // TODO
    }
    // Closes the database connection
    finally { myConnection.Close(); } 
  }

  // This event is fired when the Selection is made in MasterDataGrid
  private void ViewSelectionDetails(object sender, System.EventArgs e)
  {
    // Gets the primary key value from the Master DataGrid
    Int32 categoryID = 
         Convert.ToInt32(MasterGrid.SelectedItem.Cells[0].Text); 
    Label2.Text = categoryID.ToString();
    // Sends the key to the Slave DataGrid
    PopulateSlaveGrid( categoryID );

  }
  // This method populates Slave DataGrid
  public void PopulateSlaveGrid(Int32 cID) 
  { 
    // SQL QUERY to view the selected Item details
    string commandString = 
      "SELECT CategoryName,Description FROM Categories WHERE CategoryID="+cID; 

    // Make an instance of SqlDataAdapter and Assign it the query string
    SqlDataAdapter ad = new SqlDataAdapter(commandString,myConnection); 
    // DataSet instance created
    DataSet ds = new DataSet(); 
    // Fill DataSet 
    ad.Fill(ds,"Categories"); 
    try 
    { 
      // Opens database connection
      myConnection.Open(); 
      // Populate SlaveGrid
      SlaveGrid.DataSource = ds; 
      // Binds SlaveGrid to the page
      SlaveGrid.DataBind(); 
    } 

    // Catch SqlException 
    catch ( SqlException SqlEx ) 
    { 
      // TODO
    }
    // Catch general Exception
    catch ( Exception ex ) 
    {
      // TODO
    }
    // Closes the database connection
    finally { myConnection.Close(); } 
  } 
}

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

azamsharp

Web Developer

United States United States

Member

I am the founder of knowledge base website, HighOnCoding, GridViewGuy, RefactorCode.com and ScreencastADay.com.
 
HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.
 
HighOnCoding.com www.HighOnCoding.com
 

My Blog:

Blog

 

Buy my iPhone app ABC Pop


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
GeneralAn alternative approach... PinmemberRajib Ahmed13:05 4 Dec '07  
QuestionA student of Hummmm? PinsussAnonymous7:55 11 Aug '04  
AnswerRe: A student of Hummmm? Pinmemberazamsharp11:34 11 Aug '04  
GeneralRe: A student of Hummmm? PinsussAnonymous8:18 12 Aug '04  
GeneralRe: A student of Hummmm? Pinmemberazamsharp8:39 12 Aug '04  
GeneralRe: Silly, Anonymous Ad Hominem Attacks PinmemberLemmsjid5:49 13 Aug '04  
GeneralSecurity Pinmemberhmlinder2:00 11 Aug '04  
GeneralRe: Security Pinmemberazamsharp11:35 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 11 Aug 2004
Article Copyright 2004 by azamsharp
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid