Reputation: 247
I have a GrdiView
on an asp.net page. I am setting the datasource to a DataTable
. I need to set HorizontalAlign
to left if the value in the cell is of type string, else if it is a decimal, I want it to be aligned to the right. Please let me know how to implement this. The code for the GridView
is as follows:
<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>
Also, the code for setting the DataSource
is as follows:
gridViewReport.DataSource = _reportTable;
gridViewReport.DataBind();
Upvotes: 1
Views: 14292
Reputation: 1448
you can handle it gridViewReport_ondatabound events.
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;
}
}
}
}
Upvotes: 0
Reputation: 4361
if each of your columns contain data of the same type you would already know which columns carry decimal and which don't
so considering your DataTable
has two columns and first column is carrying string and second column is carrying decimal you can do
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;
}
}
else if your column carries text which can either be a decimal or string you can do this (say the second column in DataTable
has text which can potentially contain decimal format as well)
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;
}
}
Upvotes: 2
Reputation: 3045
You can handle gridViewReport_ItemCreated() event and set up alignment of various columns as
e.Item.Cells[<zero-based-index-of-column>].Style["text-align"] = "right";
Upvotes: 1