Hi vishalkal,
Please refer below sample.
According to your requiremet modified code taking reference to the below code.
Namespaces
C#
using System.Data;
using System.Windows.Forms;
using System.Globalization;
VB.Net
Imports System.Globalization
Code
C#
private void BindGrid()
{
dataGridView1.AutoGenerateColumns = false;
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id", typeof(string)),
new DataColumn("Ammount", typeof(int)) ,
new DataColumn("Item", typeof(string))});
dt.Rows.Add("1", 11, "Mango");
dt.Rows.Add("2", 20, "Banana");
dt.Rows.Add("3", 32, "Grapes");
dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Id";
dataGridView1.Columns[0].HeaderText = "Id";
dataGridView1.Columns[0].DataPropertyName = "Id";
dataGridView1.Columns[1].HeaderText = "Ammount";
dataGridView1.Columns[1].Name = "Ammount";
dataGridView1.Columns[1].DataPropertyName = "Ammount";
dataGridView1.Columns[2].Name = "Item";
dataGridView1.Columns[2].HeaderText = "Item";
dataGridView1.Columns[2].DataPropertyName = "Item";
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex != this.dataGridView1.NewRowIndex)
{
int a = Convert.ToInt16(e.Value);
e.Value = a.ToString("C2", CultureInfo.CurrentCulture);
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.BindGrid();
}
VB.Net
Private Sub BindGrid()
dataGridView1.AutoGenerateColumns = False
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(String)), New DataColumn("Ammount", GetType(Integer)), New DataColumn("Item", GetType(String))})
dt.Rows.Add("1", 11, "Mango")
dt.Rows.Add("2", 20, "Banana")
dt.Rows.Add("3", 32, "Grapes")
dataGridView1.AutoGenerateColumns = False
dataGridView1.ColumnCount = 3
dataGridView1.Columns(0).Name = "Id"
dataGridView1.Columns(0).HeaderText = "Id"
dataGridView1.Columns(0).DataPropertyName = "Id"
dataGridView1.Columns(1).HeaderText = "Ammount"
dataGridView1.Columns(1).Name = "Ammount"
dataGridView1.Columns(1).DataPropertyName = "Ammount"
dataGridView1.Columns(2).Name = "Item"
dataGridView1.Columns(2).HeaderText = "Item"
dataGridView1.Columns(2).DataPropertyName = "Item"
dataGridView1.DataSource = dt
End Sub
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs)
If e.ColumnIndex = 1 AndAlso e.RowIndex <> Me.dataGridView1.NewRowIndex Then
Dim a As Integer = Convert.ToInt16(e.Value)
e.Value = a.ToString("C2", CultureInfo.CurrentCulture)
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.BindGrid()
End Sub
Screenshot