how to freeze left three columns of a datagridview in

Posted on 2009-02-17
Last Modified: 2012-05-06
I want to freeze left three columns of datagridview in It is a readily available property in WinForm, but how to make it in Please suggest.

Any javascript code snippet or  CSS code is also welcome.

Thanking you,
Question by:Amitava_Mukherjee
    LVL 2

    Expert Comment

    I know this will work in IE5+.  I havent tested in other browsers, but you should be able to modify.

    There might be more elegant ways, but I use this method to lock the head or cols.

    Be sure to put your datagrid in a div called <div id="tbl-container">
    Also, be sure to add this to the grid properties - OnItemDataBound="myGrid_ItemDataBound"
    //asp code behind
    public void myGrid_ItemDataBound(Object sender, DataGridItemEventArgs e)
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
         e.Item.Cells[0].Attributes.Add("class", "locked");
         e.Item.Cells[1].Attributes.Add("class", "locked");
         e.Item.Cells[2].Attributes.Add("class", "locked");
    //style sheet
    cursor: default; 	
    top: expression(document.getElementById("tbl-container").scrollTop-2); /* IE5+ only */
    z-index: 20;
    td.locked,  th.locked{
    left: expression(document.getElementById("tbl-container").scrollLeft); /* IE5+ only */
    position: relative;
    z-index: 10;

    Open in new window


    Author Comment

    left: expression(document.getElementById("tbl-container").scrollLeft);

    this line generate error in CSS. Error returns "tbl-container is not a valid value for the list property"
    LVL 2

    Expert Comment

    Do you mean "tbl-container is not a valid value for the left property?"

    My CSS in VS says that also.  But it still works.  Is the page not loading at all?

    Author Comment

    the page is loading and showing all the things without any error. but no effect of the css

    I'd specified the the css in my seleted theme's css file
    LVL 2

    Accepted Solution

    Just to be sure...did you put your datagrid in the div like this -
    <div id="tbl-container">
         <asp:GridView id="mygrid" OnItemDataBound="myGrid_ItemDataBound" ... />

    Author Closing Comment


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
    In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now