How can I format cells of a Detail View alignment?

Hi, I'm using asp.net 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.
lapuccaAsked:
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)">
        <SelectParameters>
            <asp:SessionParameter Name="UserID" SessionField="UserID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

Open in new window

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


example

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


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;
            }
            else
            {
                       cell.HorizontalAlign = HorizontalAlign.Left;
            }
        }
    }
}            
0
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:
Sona,
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.
Implementation.aspx
Implementation.aspx.cs
0
 
lapuccaAuthor Commented:
Also, is there a way to set padding for all cells in the DetailView control?  Thanks.
0
 
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">
    <Fields>
        <asp:BoundField DataField="jKeepGoal" HeaderText="Keep Goal">
             <itemstyle HorizontalAlign="Center">
              </itemstyle>
         </asp:boundfield>
        <asp:BoundField DataField="lBreakfast" HeaderText="Break fast" >
             <itemstyle HorizontalAlign="Left">
              </itemstyle>
         </asp:Boundfield>
       
    </Fields>
</asp:DetailsView>

For padding you can use cellpadding property of detailview

cellpadding="10"
0
 
ExpertLogin_890Commented:
Hi,

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

http://it.toolbox.com/blogs/codesharp/detailsview-on-steroids-20231

0
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.