• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

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

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
lapucca
Asked:
lapucca
1 Solution
 
Ramkisan JagtapLead DeveloperCommented:
Can you please post your gridview code?
0
 
Easwaran ParamasivamCommented:
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
 
lapuccaAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Ramkisan JagtapLead DeveloperCommented:
Remove following attribute from GridView
RowStyle-HorizontalAlign="Right"
0
 
lapuccaAuthor Commented:
If I do that then all columns become left aligned.  I only want the last column to be left aligned.  Thank you.
0
 
Ramkisan JagtapLead DeveloperCommented:
Please  try this in codebehind
gvBiWeekly.Columns["comments"].ItemStyle.HorizontalAlign = HorizontalAlign.Left;

Regards,
Ram
0
 
lapuccaAuthor Commented:
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
 
Ramkisan JagtapLead DeveloperCommented:
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
 
lapuccaAuthor Commented:
Yes, that worked.  Thank you.
0
 
AlokJain0412Commented:
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
 
Ramkisan JagtapLead DeveloperCommented:
@AlokJain0412 : Exactly what do you want to mention with above code?
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now