Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How can I set the last column of a gridview to Left Aligned?

Posted on 2011-10-02
11
Medium Priority
?
368 Views
Last Modified: 2012-05-12
Hi, I'm using asp.net 3.5 and C#
I bind a gridiview to a sql sqldatasource at turn because I get the item ID to be part of the select commnad for the sqldatasource control.  I also set the RowStyle-HorizontalAlign property of the gridview control in the Design's Properties.  that works for all columns in this except for the last column which is a text column.  How can I set it to be Left Aligned?  Thank you
0
Comment
Question by:lapucca
11 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36901516
Can you please post your gridview code?
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36901521
In the column definition mention the alignment as shown in below example.
<asp:BoundField HeaderText="First Name"  DataField="firstName" ItemStyle-HorizontalAlign="Left" />

Open in new window

0
 

Author Comment

by:lapucca
ID: 36901670
attached are the code files.  I want to make the last column, Comment, to be left aligned because it's a text field while all other columns are numbers or dates.  how can I specify that at run time.  I don't specify this at design because I need first to get the studyid from session in Page_Load event.  Thank you.
BiWeekly.aspx
BiWeekly.aspx.cs
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36901680
Remove following attribute from GridView
RowStyle-HorizontalAlign="Right"
0
 

Author Comment

by:lapucca
ID: 36901730
If I do that then all columns become left aligned.  I only want the last column to be left aligned.  Thank you.
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36901807
Please  try this in codebehind
gvBiWeekly.Columns["comments"].ItemStyle.HorizontalAlign = HorizontalAlign.Left;

Regards,
Ram
0
 

Author Comment

by:lapucca
ID: 36901864
Odd enough but Columns only accepts int index.  So I add this line of code in the Page_Load event but then I get a runtime error because the gvBiWeekly.Columns.Count is 0 even after I bind the data.  

                gvBiWeekly.DataSource = sds;
                gvBiWeekly.DataKeyNames = new string[] { "StudyId" };
                gvBiWeekly.DataBind();
                gvBiWeekly.Columns[gvBiWeekly.Columns.Count - 1].ItemStyle.HorizontalAlign = HorizontalAlign.Left;
0
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 2000 total points
ID: 36901886
please add rowDataBound Event for your grid and try wuth following code

protected void gvBiWeekly_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[e.Row.Cells.Count - 1].HorizontalAlign = HorizontalAlign.Left;
               
            }
        }
0
 

Author Closing Comment

by:lapucca
ID: 36901901
Yes, that worked.  Thank you.
0
 
LVL 5

Expert Comment

by:AlokJain0412
ID: 36908939
It is The sample code do accordingly
This is working fine here  
 



Open in new window

<Columns>
                                               
                                                <asp:BoundField DataField="comp_waranty_month" ItemStyle-HorizontalAlign="NotSet" HeaderText="Warranty Month" />
                                                <asp:BoundField DataField="due_days" ItemStyle-HorizontalAlign="NotSet" HeaderText="Due Days" />
                                                <asp:BoundField DataField="item_name" ItemStyle-HorizontalAlign="left"  HeaderText="Component Name" />
                                            </Columns>
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36908975
@AlokJain0412 : Exactly what do you want to mention with above code?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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!
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
Suggested Courses

581 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