Horizo​​ntalAlign in gridview动态

时间:2012-05-28 10:34:02

标签: c# asp.net

我在asp.net页面上有一个GrdiView。我将数据源设置为DataTable。如果单元格中的值是string类型,我需要将HorizontalAlign设置为left,否则如果它是小数,我希望它与右边对齐。请让我知道如何实现这一点。 GridView的代码如下:

      <asp:GridView ID="gridViewReport" runat="server" Height="1px"       OnRowDataBound="UsersGVRowDataBound" GridLines="both" Style="z-index: 100; 
        left: 2px; position: absolute; top: 1px; " Width="939px" CellPadding="4"   ForeColor="#333333" AllowSorting="True" OnSorting="gridViewReport_Sorting" Font-Names="Verdana" Font-Size="12px" OnSelectedIndexChanged="gridViewReport_SelectedIndexChanged1">
        <AlternatingRowStyle BackColor="White" />
        <RowStyle HorizontalAlign="Left" BackColor="#EFECE5" />            
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#C0F6C0" ForeColor="#333333" Font-Bold="True" />
        <HeaderStyle BackColor="#CDE472"  Font-Bold="True" ForeColor="DarkGreen" HorizontalAlign ="Left"  Font-Italic="False" Font-Names="Verdana" Font-Overline="False"/>
        <EditRowStyle BackColor="#EAEAAE" />            
    </asp:GridView> 

此外,设置DataSource的代码如下:

gridViewReport.DataSource = _reportTable;
gridViewReport.DataBind();

3 个答案:

答案 0 :(得分:2)

如果你的每一列包含相同类型的数据,你就会知道哪些列带有小数,哪些列不带

所以考虑到你的DataTable两列,第一列是携带字符串,第二列是带小数,你可以做

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

否则,如果您的列包含可以是小数或字符串的文本,则可以执行此操作(例如DataTable中的第二列包含文本,其中可能包含十进制格式

    protected void gridViewReport_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            decimal val;
            if(decimal.TryParse(e.Row.Cells[1].Text, out val))
                e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            else
                e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Left;
        }
    }

答案 1 :(得分:1)

您可以处理gridViewReport_ItemCreated()事件并将各列的对齐设置为

e.Item.Cells[<zero-based-index-of-column>].Style["text-align"] = "right";

答案 2 :(得分:0)

你可以处理它的gridViewReport_ondatabound事件。

protected void gridViewReport_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            decimal cellValue = 0.0m;
            DataRow dr = ((DataRowView)e.Row.DataItem).Row;

            if (dr[urs_cell_index] != null)
            {
                if (decimal.TryParse(dr[urs_cell_index].ToString(), out cellValue))
                {
                    e.Row.Cells[urs_cell_index].HorizontalAlign = HorizontalAlign.Right;
                }
                else
                {
                    e.Row.Cells[urs_cell_index].HorizontalAlign = HorizontalAlign.Left;
                }
            }
        }
    }