How can I format cells of a Detail View alignment?

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.
Who is Participating?
Kiran SonawaneProject LeadCommented:
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

Kiran SonawaneProject LeadCommented:
For alignment show your detailview code in aspx file
Kumaraswamy RCommented:


<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;
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

lapuccaAuthor Commented:
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.
lapuccaAuthor Commented:
Also, is there a way to set padding for all cells in the DetailView control?  Thanks.
Kiran SonawaneProject LeadCommented:
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


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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.