【问题标题】:.NET GridView - Can you right-align just one column?.NET GridView - 你能只右对齐一列吗?
【发布时间】:2011-08-04 09:15:03
【问题描述】:

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

我有这个

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

它绑定到具有许多列的 DataTable(动态生成)。我只希望“价格”列右对齐。

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

【问题讨论】:

  • 对价格列使用右对齐模板字段。可能吗?

标签: c# asp.net gridview


【解决方案1】:

是的,您可以,但我认为如果您将 AutoGenerateColumns 设置为 true(默认情况下),那么您需要使用 RowDataBound 事件右对齐列。附带说明一下,如果更简单,您可以将 AutoGenerateColumns 设置为 false 并使用 BoundFields,这将为您提供更多格式选项,并且可能会消除对 RowDataBound 事件的需要。

网格视图:

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

希望对您有所帮助。

【讨论】:

  • 我明白了...是的,正如我所料,它不是很漂亮。 AutoGenerateColumns 是我使用 GridView 的原因。我宁愿打印出 HTML,也不愿使用禁用 AutoGenerateColumns 的 GridView。谢谢!这回答了我的问题:)
【解决方案2】:
<Columns>
...
    <asp:BoundField DataField="Price" HeaderText="Price" 
        ItemStyle-HorizontalAlign="Right" ItemStyle-Width="80" />
...
</Columns>

【讨论】:

  • 这将添加一个额外的列,而不是格式化我想要的列
  • 在标记中添加这样的列。除非你有特定的理由想要在你的代码隐藏中这样做?
  • 要使用这种方法,您需要将 AutoGenerateColumns 设置为 false 并手动添加标记中的所有列。缺点是打字很多。好处是您可以更好地控制格式,而不必求助于代码隐藏。我个人觉得使用 BoundFields 是一种更好的方法。
  • AutoGenerateColumns 是我使用 GridView 的原因。我宁愿打印出 HTML(并完全控制)而不是使用禁用 AutoGenerateColumns 的 GridView。你怎么看?
  • @asimili,如果您想要更干净的 HTML,请删除 GridView 并改用 ListView。由于这个原因,我这些天专门使用 ListView :)
【解决方案3】:

将 ItemTemplate 中的项目包含在一个 div 中,然后为该 div 设置样式。

<ItemTemplate>
<div id="divReportName">
<asp:Label ID="lblReport" runat="server" ></asp:Label>
</div>
</ItemTemplate>

// css for div
#divReportName { text-align: left;}

【讨论】:

    【解决方案4】:

    您可以使用 ItemStyle 在 Boundfield 内执行对齐-

    像这样:

    <asp:BoundField DataField="SOH" HeaderText="SOH" SortExpression="SOH" ItemStyle-HorizontalAlign="Right"/>
    

    这对我有用,我只需要对齐我的 gridview 中的特定列

    【讨论】:

      【解决方案5】:

      您是否在GridView 的第一行添加了这个?

      OnRowDataBound="GridView1_RowDataBound" 
      

      【讨论】:

        【解决方案6】:

        即使问题很久以前发布,这可能会帮助您碰巧最终访问此页面的人。

        给出的答案假定将应用对齐的列的索引是预先知道的,或者这些列是在设计时在 .aspx 页面上创建的;但情况并非总是如此。

        对于寻找通用解决方案的人,其中列是自动生成的,并且列的索引标题为“价格”事先未知 strong>,这里有一个解决方案

        protected void grData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = ((DataTable)((GridView)sender).DataSource).Columns.IndexOf("Price");
                for (int j = 0; j < e.Row.Cells.Count; j++)
                {
                    if (j == i)
                        e.Row.Cells[j].HorizontalAlign = HorizontalAlign.Right;
                    else
                        e.Row.Cells[j].HorizontalAlign = HorizontalAlign.Left;
                }
           }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-29
          • 2021-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多