C# 使 Gridview 列居中并右对齐 (MiddleRight)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12364876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:00:59  来源:igfitidea点击:

Make of the Gridview Column Center and Right Align (MiddleRight)

c#asp.netgridviewalignment

提问by Krishna Thota

I need to align the values inside the Grid View to to Center and Right. Because I need to align Currency.

我需要将网格视图中的值对齐到中心和右侧。因为我需要调整货币。

And other Columns need to be aligned Center and Left.

其他列需要居中和居左对齐。

Can you Help me doing This??

你能帮我做这个吗??

I'm currently Doing This.

我目前正在做这个。

<asp:TemplateField ItemStyle-HorizontalAlign="Right" HeaderText="Amount">
    <ItemTemplate>Rs.<%# Eval("Payable_Bill_Amount","{0:n}")%></ItemTemplate>
</asp:TemplateField>

This Aligns the Content to totally Right. I need to align the Content to Right But Also to Center Like Calculations We Do

这将内容对齐到完全正确。我需要将内容向右对齐,但也要像我们所做的计算一样居中

Ex:

前任:

 Required          Current

|    10.00  |   |     10.00|

|   110.00  |   |    110.00| 

|  1210.00  |   |   1210.00|
----------

采纳答案by chead23

How about aligning them to the right using

如何将它们对齐使用

ItemStyle-HorizontalAlign="Right"

and then using CSS padding to add some space around the edge of the value in the cell?

然后使用 CSS 填充在单元格中值的边缘周围添加一些空间?

回答by Derek

EDITED

已编辑

This is what you need.

这就是你所需要的。

 GridView1.Columns[0].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
 GridView1.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Middle;

This should work.

这应该有效。

回答by joedotnot

Above accepted answer did not work for me. Although i did not try it in code as per above answer, i tried using the equivalent in the aspx page (which did not work).

以上接受的答案对我不起作用。虽然我没有按照上面的答案在代码中尝试它,但我尝试在 aspx 页面中使用等效项(这不起作用)。

<asp:BoundField etc>
  <ItemStyle etc />
  <HeaderStyle HorizontalAlign="Right" />
</asp:BoundField>

What worked for me was

对我有用的是

<HeaderStyle CssClass="colHeader-RightAlign" />

and created a style within the <head>

并在其中创建了一种风格 <head>

<style type="text/css">
    .colHeader-RightAlign {
        text-align:right !important;
    }
</style>

回答by Er. Harry Singh

Try this for perticuler column alignment

试试这个用于 perticuler 列对齐

protected void myGridView_RowDataBound(object o, GridViewRowEventArgs e)
{
        // Set the alignment of cells containing numeric data.
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
        }
}