.NET GridView - 你能右对齐一列吗? [英] .NET GridView - Can you right-align just one column?

查看:30
本文介绍了.NET GridView - 你能右对齐一列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否轻松地将 GridView 中的一列右对齐?

Can you easily right-align just one column in a GridView?

我有这个

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

它绑定到一个有很多列的数据表(动态生成).我只希望价格"列右对齐.

It is bound to a DataTable (generated dynamically) that has many columns. I just want the 'Price' column to be right-aligned.

(遇到这个问题,我想知道我是否应该打印出 HTML <table> 而不是使用 GridView.使用 HTML 我可以完全控制.)

(Coming across this problem, I am wondering if I should be printing out HTML <table> instead of using a GridView. Using HTML I would have total control.)

推荐答案

是的,您可以,但我认为如果您将 AutoGenerateColumns 设置为 true(它是默认情况下)然后您需要使用 RowDataBound 事件右对齐列.作为旁注,如果更简单,您可以将 AutoGenerateColumns 设置为 false 并使用 BoundFields 这将为您提供更多格式选项,并且可能会消除对 RowDataBound 事件的需要.

Yes, you can, but I think if you have AutoGenerateColumns set to true (which it is by default) then you need to right align the column using the RowDataBound event. As a side note, if it's easier you can set AutoGenerateColumns to false and use BoundFields which will give you more formatting options and will probably eliminate the need for the RowDataBound event.

网格视图:

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" runat="server"></asp:GridView>

代码隐藏:

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;
}

希望有所帮助.

这篇关于.NET GridView - 你能右对齐一列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆