datagrid and border-collapse:collapse style

Discussion in 'ASP .Net' started by Vaclav Jedlicka, Jun 26, 2003.

  1. Hi

    I need a datagrid on a page, but it is rendered with the style
    "border-collapse:collapse;". I do not need this style. It interferes with
    the settings in my CSS file. I tried to supress it with this code (1 line of
    code):

    DataGrid1.ControlStyle.Reset();

    which works.
    Unfortunately I need cellspacing="0"
    When I set cellspacing to zero like this (2 lines of code):

    DataGrid1.ControlStyle.Reset();
    DataGrid1.CellSpacing = 0;

    then the datagrid is rendered as a table and the
    style="border-collapse:collapse;" comes back again!

    How can I get rid of it?


    Thanks

    Vaclav
    Vaclav Jedlicka, Jun 26, 2003
    #1
    1. Advertising

  2. Hello Vaclav,

    I have seen this question in the group. Here is the reply from ASP.NET
    Development team:

    ----------------------------------------------------------
    No there isn't a way to get rid of border-collapse if you set CellSpacing
    to 0.

    Perhaps there should have been a way to override this behavior. Tables
    with cellspacing=0 with the borders collapsed don't look like they have
    really 0 cellspacing (visually), because each cell has a border. Therefore
    to make it appear that there is absolutely no space between cells, we add
    this style attribute.

    Here's what you should do:
    1. Write a MyTable control deriving from Table
    2. In there override CreateControlStyle to plug in a derived style
    protected override Style CreateControlStyle() {
    return new MyTableStyle(ViewState);
    }
    3. Write the MyTableStyle class deriving from TableStyle like so:

    public class MyTableStyle : TableStyle {
    private bool _rendering;

    public override int CellSpacing {
    get {
    if (_rendering) {
    return -1;
    }
    return base.CellSpacing;
    }
    set {
    base.CellSpacing = value;
    }
    }

    public override void AddAttributesToRender(HtmlTextWriter writer,
    WebControl owner) {
    try {

    _rendering = true;
    base.AddAttributesToRender(writer, owner);
    }
    finally {
    _rendering = false;
    }
    int n = CellSpacing;
    if (n >= 0) {
    writer.AddAttribute(HtmlTextWriterAttribute.CellSpacing,
    n.ToString(CultureInfo.InvariantCulture));
    }
    }
    }

    That should do the trick... of course this is email code based on memory of
    the code, so it might need small modifications to fully work.

    -------------------------------------------------

    Hope it helps.

    Best regards,
    yhhuang
    VS.NET, Visual C++
    Microsoft

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Got .Net? http://www.gotdotnet.com
    --------------------
    !From: "Vaclav Jedlicka" <>
    !Subject: datagrid and border-collapse:collapse style
    !Date: Thu, 26 Jun 2003 12:47:12 +0200
    !Lines: 28
    !X-Priority: 3
    !X-MSMail-Priority: Normal
    !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    !X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    !Message-ID: <OdZQ$>
    !Newsgroups: microsoft.public.dotnet.framework.aspnet
    !NNTP-Posting-Host: 195.47.25.99
    !Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
    !Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:155060
    !X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    !
    !Hi
    !
    !I need a datagrid on a page, but it is rendered with the style
    !"border-collapse:collapse;". I do not need this style. It interferes with
    !the settings in my CSS file. I tried to supress it with this code (1 line
    of
    !code):
    !
    !DataGrid1.ControlStyle.Reset();
    !
    !which works.
    !Unfortunately I need cellspacing="0"
    !When I set cellspacing to zero like this (2 lines of code):
    !
    !DataGrid1.ControlStyle.Reset();
    !DataGrid1.CellSpacing = 0;
    !
    !then the datagrid is rendered as a table and the
    !style="border-collapse:collapse;" comes back again!
    !
    !How can I get rid of it?
    !
    !
    !Thanks
    !
    !Vaclav
    !
    !
    !
    !
    Yan-Hong Huang[MSFT], Jun 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    0
    Views:
    2,235
    tshad
    Jan 31, 2005
  2. Michael Tissington

    asp:Table and border-collapse

    Michael Tissington, Aug 1, 2003, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    240
    Michael Tissington
    Aug 5, 2003
  3. VVSRKS

    How can I avoid border-collapse:collapse to come ...

    VVSRKS, Sep 16, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    185
    VVSRKS
    Sep 16, 2004
  4. Tom Castiglia

    Why does Gridview ALWAYS emit style="border-collapse: collapse;"

    Tom Castiglia, Jun 4, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    1,932
    Tom Castiglia
    Jun 9, 2006
  5. M.L.
    Replies:
    9
    Views:
    284
Loading...

Share This Page