Click here to Skip to main content
15,885,365 members
Articles / Web Development / ASP.NET

GridView inline Master/Detail record display

Rate me:
Please Sign up or sign in to vote.
4.60/5 (44 votes)
17 Feb 20066 min read 745.2K   11.7K   280  
How to view detail records inline with the master records, using the GridView control.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

	}
	protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		string KeyValue = GridView1.DataKeys[e.RowIndex].Value.ToString();
		if (KeyValue != "0")
			return; // key value of 0 indicates the insert row

		System.Web.UI.WebControls.SqlDataSource ds = (System.Web.UI.WebControls.SqlDataSource)this.FindControl(this.GridView1.DataSourceID);
		System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ds.ConnectionString);
		conn.Open();
		string s = ds.InsertCommand;
		System.Data.SqlClient.SqlCommand c = new System.Data.SqlClient.SqlCommand(s, conn);

		System.Data.SqlClient.SqlParameter p;
		foreach (System.Collections.DictionaryEntry x in e.NewValues)
		{
			p = new System.Data.SqlClient.SqlParameter("@" + x.Key, x.Value);
			c.Parameters.Add(p);
		}
		p = new System.Data.SqlClient.SqlParameter("@CategoryID", Request.QueryString["ID"]);
		c.Parameters.Add(p);
		c.ExecuteNonQuery();
	}

	void AddGlyph(GridView grid, GridViewRow item)
	{
		if (grid.AllowSorting == false)
			return;
		Label glyph = new Label();
		glyph.EnableTheming = false;
		glyph.Font.Name = "webdings";
		glyph.Font.Size = FontUnit.XSmall;
		glyph.Text = (grid.SortDirection == SortDirection.Ascending ? "5" : " 6");

		// Find the column you sorted by
		for (int i = 0; i < grid.Columns.Count; i++)
		{
			string colExpr = grid.Columns[i].SortExpression;
			if (colExpr != "" && colExpr == grid.SortExpression)
			{
				item.Cells[i].Controls.Add(glyph);
			}
		}
	}

	protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.Header)
			AddGlyph(GridView1, e.Row);
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			string KeyValue = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
			if (KeyValue == "0" && EditIndex == -1)		// we are not editing
				e.Row.Attributes.Add("isadd", "1");
		}
	}

	public int EditIndex = -1;
	protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
	{
		EditIndex = e.NewEditIndex;
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions