Tuesday, December 21, 2010

Expand-Collapse Gridview

Well I used to display data in the expand/expand gridview

protected void gdvwplansdetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

ClsBO.ClsPlanBO objclsbo = new ClsBO.ClsPlanBO();
DataSet ds = objclsbo.GetCoursePlans(Convert.ToInt64(gdvwplansdetails.DataKeys[e.Row.RowIndex].Value.ToString()));
//string DetailsQuery = "SELECT p.title,p.duration,p.price,c.thumbnail_path FROM [tbl_bt_lesson_plan] as p, [tbl_bt_course] as c where p.plan_id = c.plan_id and p.plan_id = '18'";


//Here I am grabbing the additional data and putting it into mini datagrids...
//If you wish to just use labels, or other controls, just bind the data as you
//wish, and render to html as I did.
GridView NewDg = new GridView();
NewDg.AutoGenerateColumns = false;
NewDg.CellSpacing = 0;
NewDg.CellPadding = 0;
NewDg.Width = 782;
NewDg.ShowHeader = false;
NewDg.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
NewDg.BorderColor = System.Drawing.Color.White;
if (ds.Tables[0].Rows.Count > 0)
{
TemplateField empty = new TemplateField();
empty.ItemStyle.Width = 42;
NewDg.Columns.Add(empty);
ImageField crseimage = new ImageField();
crseimage.DataImageUrlField = "imagepath";
crseimage.DataAlternateTextField = "coursetitle";
crseimage.ItemStyle.Width = 160;
crseimage.ControlStyle.Width = 45;
crseimage.ControlStyle.Height = 45;
NewDg.Columns.Add(crseimage);
BoundField title = new BoundField();
title.ItemStyle.Width = 430;
title.DataField = "coursetitle";
NewDg.Columns.Add(title);
BoundField time = new BoundField();
time.DataField = "duration";
time.ItemStyle.Width = 104;
NewDg.Columns.Add(time);
BoundField price = new BoundField();
price.DataField = "cost";
price.ItemStyle.Width = 100;
price.DataFormatString = "{0:c}";
price.NullDisplayText = "FREE";
NewDg.Columns.Add(price);
}
else
NewDg.EmptyDataText = "No Courses Found";

NewDg.DataSource = ds;
NewDg.DataBind();

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
NewDg.RenderControl(htw);

string DivStart = "";
string FullDIV = DivStart + DivBody + DivEnd;

int LastCellPosition = e.Row.Cells.Count - 1;
int NewCellPosition = e.Row.Cells.Count - 2;

e.Row.Cells[0].ID = "CellInfo" + e.Row.RowIndex.ToString();

////Match color of div which we will expand base on row
if (e.Row.RowIndex % 2 == 0)
{
//set to regular row style
e.Row.Cells[LastCellPosition].Text = e.Row.Cells[LastCellPosition].Text + "" + FullDIV;
}
else
{
//set to alternative row style
e.Row.Cells[LastCellPosition].Text = e.Row.Cells[LastCellPosition].Text + "" + FullDIV;
}

e.Row.Cells[0].Attributes["onclick"] = "HideShowPanel('uniquename" + e.Row.RowIndex.ToString() + "'); ChangePlusMinusText('" + e.Row.Cells[0].ClientID + "'); SetExpandedDIVInfo('" + e.Row.Cells[0].ClientID + "','" + this.txtExpandedDivs.ClientID + "', 'uniquename" + e.Row.RowIndex.ToString() + "');";
e.Row.Cells[0].Attributes["onmouseover"] = "this.style.cursor='pointer'";
e.Row.Cells[0].Attributes["onmouseout"] = "this.style.cursor='pointer'";

}
}

Please refer below image for your reference

No comments:

Post a Comment