Freezing Columns

Topics: Developer Forum, User Forum
Sep 18, 2007 at 2:02 PM
I could have sworn that I saw this as a comment somewhere on this site but now that I need it I can't find it. How would I freeze a column when scrolling to the right. For example the Header and the first 3 columns should always be visible. Also I'm a VB guy, although I can follow what is going on in C#, witting my own additions would be a little shaky.

So, can anyone give an example or point me to the Comment that I can't seem to find about freezing columns. And does anyone have a copy of the Dll's code written in VB?

Thanks a bunch
-- Joe --
Sep 19, 2007 at 3:58 PM
I've managed to get this working but I still can't get Visual Studio to recognize my newly created Property "LockColumn". Which simply is the number of the column to lock, so everything to the left is always visible.

I tried to paste the whole of the code and I ran out of space, so here is what I added:

private const string FrozenTopCssClass = "frozenTop";
//* ** New
private const string FrozenColCssClass = "frozenCol";
private const string FrozenHeaderColCssClass = "frozenHeaderCol";

//* ** My new property
Browsable(true), DefaultValue("0"), Category("Behavior")
public string LockColumn
{
get
{
object val = this.ViewState"LockColumn";
if (null == val)
{
return "0";
}
return (string)val;
}
set
{
this.ViewState"LockColumn" = value;
}
}

protected override void OnPreRender(EventArgs e)
{
...... Existing Code .....
//* ** My Addition
if (this.FreezeHeader && !this.Page.Items.Contains(FrozenGridView.FrozenColCssClass))
{
this.Page.ItemsFrozenGridView.FrozenColCssClass = "2";
FrozenColStyle frozenColStyle = new FrozenColStyle();
this.Page.Header.StyleSheet.CreateStyleRule(frozenColStyle, null, "." + FrozenGridView.FrozenColCssClass);
}

if (this.FreezeHeader && !this.Page.Items.Contains(FrozenGridView.FrozenHeaderColCssClass))
{
this.Page.ItemsFrozenGridView.FrozenHeaderColCssClass = "2";
FrozenHeaderColStyle frozenHeaderColStyle = new FrozenHeaderColStyle();
this.Page.Header.StyleSheet.CreateStyleRule(frozenHeaderColStyle, null, "." + FrozenGridView.FrozenHeaderColCssClass);
}


private void FreezeCols()
{
if (this.FreezeHeader)
{
foreach (GridViewRow tr in this.Rows)
{
for (int i = 0; i <= System.Convert.ToInt32(LockColumn); i++)
{
tr.Cellsi.CssClass = FrozenGridView.FrozenColCssClass + " " + tr.CssClass;
}
}

if (this.HeaderRow.Cells.Count > System.Convert.ToInt32(LockColumn))
{
for (int i = 0; i <= System.Convert.ToInt32(LockColumn); i++)
{
this.HeaderRow.Cellsi.CssClass = FrozenGridView.FrozenHeaderColCssClass + " " + this.HeaderRow.Cellsi.CssClass;
}
}
}
}


private class FrozenColStyle : Style
{
internal FrozenColStyle()
{
}

protected override void FillStyleAttributes(CssStyleCollection attributes, IUrlResolutionService urlResolver)
{
base.FillStyleAttributes(attributes, urlResolver);

attributesHtmlTextWriterStyle.BackgroundColor = "whitesmoke";
//attributesHtmlTextWriterStyle.Left = "expression(parentNode.parentNode.parentNode.parentNode.scrollLeft)";
attributesHtmlTextWriterStyle.Left = "expression(this.offsetParent.scrollLeft)";
attributesHtmlTextWriterStyle.Position = "relative";
attributesHtmlTextWriterStyle.ZIndex = "2";
}
}


private class FrozenHeaderColStyle : Style
{
internal FrozenHeaderColStyle()
{
}

protected override void FillStyleAttributes(CssStyleCollection attributes, IUrlResolutionService urlResolver)
{
base.FillStyleAttributes(attributes, urlResolver);

//attributesHtmlTextWriterStyle.BackgroundColor = "navy";
//attributesHtmlTextWriterStyle.Left = "expression(parentNode.parentNode.parentNode.parentNode.scrollLeft)";
attributesHtmlTextWriterStyle.Left = "expression(this.offsetParent.scrollLeft)";
attributesHtmlTextWriterStyle.Position = "relative";
attributesHtmlTextWriterStyle.ZIndex = "4";
}
}