Click here to Skip to main content
15,908,015 members
Articles / Web Development / ASP.NET
Article

How to create a simple Master-Slave DataGrid

Rate me:
Please Sign up or sign in to vote.
1.67/5 (6 votes)
10 Aug 2004 62.6K   17   8
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

C#
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


Written By
Web Developer
United States United States
My name is Mohammad Azam and I have been developing iOS applications since 2010. I have worked as a lead mobile developer for VALIC, AIG, Schlumberger, Baker Hughes, Blinds.com and The Home Depot. I have also published tons of my own apps to the App Store and even got featured by Apple for my app, Vegetable Tree. I highly recommend that you check out my portfolio. At present I am working as a lead instructor at DigitalCrafts.




I also have a lot of Udemy courses which you can check out at the following link:
Mohammad Azam Udemy Courses

Comments and Discussions

 
GeneralAn alternative approach... Pin
Rajib Ahmed4-Dec-07 13:05
Rajib Ahmed4-Dec-07 13:05 
I have an alternative approach. Just was curious what you thought of it:
http://www.progtalk.com/ViewArticle.aspx?ArticleID=1[^]

Rajib Ahmed

QuestionA student of Hummmm? Pin
Anonymous11-Aug-04 7:55
Anonymous11-Aug-04 7:55 
AnswerRe: A student of Hummmm? Pin
azamsharp11-Aug-04 11:34
azamsharp11-Aug-04 11:34 
GeneralRe: A student of Hummmm? Pin
Anonymous12-Aug-04 8:18
Anonymous12-Aug-04 8:18 
GeneralRe: A student of Hummmm? Pin
azamsharp12-Aug-04 8:39
azamsharp12-Aug-04 8:39 
GeneralRe: Silly, Anonymous Ad Hominem Attacks Pin
Lemmsjid13-Aug-04 5:49
Lemmsjid13-Aug-04 5:49 
GeneralSecurity Pin
hmlinder11-Aug-04 2:00
hmlinder11-Aug-04 2:00 
GeneralRe: Security Pin
azamsharp11-Aug-04 11:35
azamsharp11-Aug-04 11:35 

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

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