How can I format cells of a Detail View alignment?

Posted on 2011-10-17
Last Modified: 2012-08-13
Hi, I'm using 3.5, C#
I have a Detail View control that I assign sqldatasource at runtime because I need to get the parameter for sql query from session variable only at runtime.  Some fields needs to be left aligned while others need to be either center or right aligned.  How can I specified this at runtime?  Thank you.
Question by:lapucca
    LVL 17

    Accepted Solution

    You can pass the session parameter using SessionParameter like below
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:testDatabaseConnectionString %>" 
            SelectCommand="SELECT * FROM [UserTable] WHERE ([userID] = @UserID)">
                <asp:SessionParameter Name="UserID" SessionField="UserID" Type="Int32" />

    Open in new window

    LVL 17

    Expert Comment

    For alignment show your detailview code in aspx file
    LVL 29

    Assisted Solution

    by:Kumaraswamy R


    <asp:GridView ID="GridView1" runat="server" OnRowCreated="OnRowCreated" cellpadding="5">
            <asp:boundfield datafield="Name" headerstyle-forecolor=Blue />

    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.Header)
            Int32 idx = 0;
            foreach(TableCell cell in e.Row.Cells)
                cell.Text = String.Format("{0}_{1}", cell.Text, (++idx));
                if (idx%2 == 0)
                    cell.HorizontalAlign = HorizontalAlign.Right;
                           cell.HorizontalAlign = HorizontalAlign.Left;

    Author Comment

    Thank you for showing me how to setup sqldatasource in aspx using asp:sessionparameter.  Attached are the aspx and the cs files.  Also, aI noticed that all fiedls are left aligned in Firefox but in IE they're all centered which comes form the <center> in aspx file.  Wonder why they look different in different browsers.  Thanks.

    Author Comment

    Also, is there a way to set padding for all cells in the DetailView control?  Thanks.
    LVL 17

    Assisted Solution

    It seem your are adding all columns (from select statement) to details view. In order to different alignment for different columns you need to add the BoundFields to data column for instance look below code. There is item style with horizontal align

    <asp:DetailsView ID="dvImplementation" runat="server" Width="600px" CommandRowStyle-VerticalAlign="Middle" HeaderStyle-BackColor="#99FF66" BackColor="White" BorderColor="Black" BorderStyle="Solid">
            <asp:BoundField DataField="jKeepGoal" HeaderText="Keep Goal">
                 <itemstyle HorizontalAlign="Center">
            <asp:BoundField DataField="lBreakfast" HeaderText="Break fast" >
                 <itemstyle HorizontalAlign="Left">

    For padding you can use cellpadding property of detailview

    LVL 3

    Expert Comment


    For padding a cells you can set detailview control cell padding property like
    cellpadding ="4" cellspacing="5", etc

    for dynamically alignment the cells use the detailsview control onrowcreated event set the any routine to perform the alignment of cells check the below links


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    794 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

    18 Experts available now in Live!

    Get 1:1 Help Now