【发布时间】:2021-10-14 12:19:24
【问题描述】:
我有一个网格视图。我正在使用数据表在代码中动态绑定gridview。我的gridview 中只有两列,一列是文本列,另一列是数字列。我想将左列、文本列左对齐、数字列右对齐。以下是我的代码:
DataTable dt = new DataTable();
dt.Columns.Add("Fee Breakdown", typeof(string));
dt.Columns.Add(" ", typeof(string));
dt.Rows.Add("First Page", String.Format("{0:0.00}", 14));
dt.Rows.Add("Additional Page(s)", String.Format("{0:0.00}", 3));
protected void grdCalculate_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;
}
}
dt.rows.add 中的数字 14 和 3 是一个动态计算的值,位数可以增加和减少。 rowsdatabound 中的上述代码没有将列左右对齐,我正在尝试对齐列,以便所有小数都可以正确右对齐并且更容易添加数字。下面是gridview的图片。如您所见,小数点在 $14.00 和 $3.00 中没有正确对齐。如何将第一行左对齐,右行右对齐。下面是我的aspx页面代码:
<asp:GridView ID="grdCalculate" runat="server" GridLines="Horizontal" OnRowDataBound="grdCalculate_RowDataBound" ></asp:GridView>
【问题讨论】: