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

查看:689
本文介绍了.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 &LT;表&gt; ,而不是使用一个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,并使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.aspx\">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.

GridView的:

GridView:

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

codebehind:

Codebehind:

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天全站免登陆