Search This Blog

2009-08-14

GridView with Fixed Header and Freeze Column

Code Snippent:
protected void GridLoad()
{
/*
//For DB Connection and all other operation in Grid
SqlConnection sqlCon = new SqlConnection();
DPE dpe = new DPE();
sqlCon = dpe.DatabaseConnection();
SqlCommand cmd1 = new SqlCommand("MDOPEDS.OUTDATESRESULTS", sqlCon);
cmd1.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adp = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
adp.Fill(ds);
grvOutdatesData.DataSource = ds.Tables[0];
grvOutdatesData.DataBind();

Session["OutdateDataset"] = ds.Tables[0];
int colcount = ds.Tables[0].Columns.Count - 1;
int rowcount = ds.Tables[0].Rows.Count - 1;
hdn1.Value = rowcount.ToString();
for (int d = 0; d <= rowcount; d++)
{
TextBox txt = new TextBox();
txt.ID = "txtOutdates" + d;
txt.Text = grvOutdatesData.Rows[d].Cells[colcount].Text.Trim();
txt.Width = 40;
txt.Height = 12;
txt.Attributes.Add("runat='server' class", "inputTextBox1");
grvOutdatesData.Rows[d].Cells[colcount].Controls.Add(txt);
}
*/

//Freezing rows and column

for (int c = 1; c <= colcount; c++)
{
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("Width", "45px");
//gvQNDGrid.HeaderRow.Cells[c].Attributes.CssStyle.Add("writing-mode", "tb-rl");//for vertical align of header
//gvQNDGrid.HeaderRow.Cells[c].Attributes.CssStyle.Add("filter", "flipv fliph");
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("position", "relative");
grvOutdatesData.HeaderRow.Cells[c].Attributes.CssStyle.Add("TOP", "expression(this.offsetParent.scrollTop)");
}
grvOutdatesData.HeaderRow.Cells[0].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[1].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[2].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");
grvOutdatesData.HeaderRow.Cells[3].Attributes.CssStyle.Add("Width:100px;position:relative;Left:expression(this.offsetParent.scrollLeft);TOP:expression(this.offsetParent.scrollTop);Z-INDEX", "1");




foreach (GridViewRow grv in grvOutdatesData.Rows)
{
grv.Cells[0].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[0].Attributes.CssStyle.Add("position", "relative");
grv.Cells[0].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[1].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[1].Attributes.CssStyle.Add("position", "relative");
grv.Cells[1].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[2].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[2].Attributes.CssStyle.Add("position", "relative");
grv.Cells[2].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");
grv.Cells[3].Attributes.CssStyle.Add("Width", "100px");
grv.Cells[3].Attributes.CssStyle.Add("position", "relative");
grv.Cells[3].Attributes.CssStyle.Add("BACKGROUND-COLOR:#b8d0e9;LEFT", "expression(this.offsetParent.scrollLeft)");


for (int b = 0; b < colcount; b++)
{
if (grv.Cells[b].Text == " ")
{
grv.Cells[b].Attributes.CssStyle.Add("BACKGROUND-COLOR", "#f19c76");
}
}


}
}
Output:

No comments: