Hi rakeshkuma,
You need to loop through all the cells and check if decimal value then round to 2 decimal.
Refer below sample.
HTML
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="true" OnRowDataBound="OnRowDataBound">
<Columns></Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[3]
{
new System.Data.DataColumn("Id",typeof(int)),
new System.Data.DataColumn("Name",typeof(string)),
new System.Data.DataColumn("Price",typeof(decimal))
});
dt.Rows.Add(1, "Apple", "120.225");
dt.Rows.Add(2, "Banana", "48.569");
dt.Rows.Add(3, "Orange", "50");
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
decimal value;
if (decimal.TryParse(e.Row.Cells[i].Text.Trim(), out value))
{
e.Row.Cells[i].Text = Math.Round(value, 2).ToString();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As Data.DataTable = New Data.DataTable()
dt.Columns.AddRange(New Data.DataColumn(2) {
New Data.DataColumn("Id", GetType(Integer)),
New Data.DataColumn("Name", GetType(String)),
New Data.DataColumn("Price", GetType(Decimal))})
dt.Rows.Add(1, "Apple", "120.225")
dt.Rows.Add(2, "Banana", "48.569")
dt.Rows.Add(3, "Orange", "50")
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To e.Row.Cells.Count - 1
Dim value As Decimal
If Decimal.TryParse(e.Row.Cells(i).Text.Trim(), value) Then
e.Row.Cells(i).Text = Math.Round(value, 2).ToString()
End If
Next
End If
End Sub
Screenshot