You must Sign In to post a response.
  • Category: .NET

    Align currency decimals to the right side in gridview

    hi all,

    my requirement is to align the currency code to the ride side of the gridview column

    ex....
    data is align to the center in the gridview column the currency shows like

    10000.00
    100.00
    1000000.00
    10.00

    from the above example

    currency to aligned as a below


    10000.00
    100.00
    1000000.00
    10.00

    please help me to complete this task. thanks in advance.

    looking forward to hear from you..

    Thanks & Regards
    Kannan
  • #765781
    Hi,

    As per my understand the post your requirement is you want to align the currency details with right alignment, you are using Gridview column for currency details then set the "ItemStyle-HorizontalAlign="Right" " property to right, it will align the currency details to right hand side.

    If you are expecting something different then please let me know...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765792
    If you are populating the data directly into data grid view then use below code to align the text in the particular column.
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
    GridView1.Columns[1].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
    }

    OR

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
    }
    }

  • #765793
    Hai Premkumar,
    You can't align the data instead you need to set the alignment for the column. If you make the column alignment to the right, the data inside it will automatically be aligned.
    You can use something like:
    gvMyGridView.Columns[0].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #765798
    Hi
    try to change your Gridivew in Client Side



    <asp:GridView ID="Grd1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Amount" ItemStyle-HorizontalAlign="Right">
    <ItemTemplate>
    <asp:Label ID="lblAmt" runat="server" Text='<%# Eval("Amount","{0:n}")%>' ></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>



    Server Side


    DataTable Dt = new DataTable();
    DataRow dr;
    Dt.Columns.Add("Name");
    Dt.Columns.Add("Amount");


    DataColumn colString = new DataColumn("StringCol");
    colString.DataType = System.Type.GetType("System.String");
    Dt.Columns.Add(colString);

    DataColumn colDecimal = new DataColumn("DecimalCol");
    colDecimal.DataType = System.Type.GetType("System.Decimal");
    Dt.Columns.Add(colDecimal);


    dr = Dt.NewRow();
    dr[0] = "AAA";
    dr[1] = 10000.00;
    Dt.Rows.Add(dr);


    dr = Dt.NewRow();
    dr[0] = "BBB";
    dr[1] = 100.00;
    Dt.Rows.Add(dr);


    dr = Dt.NewRow();
    dr[0] = "CCC";
    dr[1] = 1000000.00;
    Dt.Rows.Add(dr);


    dr = Dt.NewRow();
    dr[0] = "DDD";
    dr[1] = 10.00;
    Dt.Rows.Add(dr);

    Grd1.DataSource = Dt;
    Grd1.DataBind();

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765799
    <asp:GridView ID="Grd1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Amount" ItemStyle-HorizontalAlign="Right">
    <ItemTemplate>
    <asp:Label ID="lblAmt" runat="server" Text='<%# Eval("Amount","{0:n}")%>' ></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765800
    You can use 'RowDataBound' event, this event gets called on each row binding , you can set your data to right aligned here

    protected void GridView1_RowDataBound(object o, GridViewRowEventArgs e)
    {
    //Assumes the Price column is at index 4
    if(e.Row.RowType == DataControlRowType.DataRow)
    e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
    }

    //OR in aspx you can set column ItemStyle as below
    < Columns>
    ...
    < asp:BoundField DataField="Price" HeaderText="Price"
    ItemStyle-HorizontalAlign="Right" ItemStyle-Width="80" />
    ...
    < /Columns>

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments