Hi smile,
You need to loop through the DataGridView rows and set the cell value by spliting.
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DisplayData();
}
private void DisplayData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Name"),
new DataColumn("Date"),
new DataColumn("Number")
});
dt.Rows.Add("A", "15.06.2021", "15-01");
dt.Rows.Add("B", "15.06.2021", "15-02");
dt.Rows.Add("C", "15.06.2021", "15-03");
dGVBrand.DataSource = dt;
foreach (DataGridViewRow row in dGVBrand.Rows)
{
if (row.Cells[2].Value != null)
{
string[] numbers = row.Cells[2].Value.ToString().Split('-');
if (numbers.Length > 1)
{
row.Cells[2].Value = row.Cells[2].Value.ToString().Split('-')[1];
}
}
}
}
}
VB.Net
Partial Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
DisplayData()
End Sub
Private Sub DisplayData()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Name"), New DataColumn("Date"), New DataColumn("Number")})
dt.Rows.Add("A", "15.06.2021", "15-01")
dt.Rows.Add("B", "15.06.2021", "15-02")
dt.Rows.Add("C", "15.06.2021", "15-03")
dGVBrand.DataSource = dt
For Each row As DataGridViewRow In dGVBrand.Rows
If row.Cells(2).Value IsNot Nothing Then
Dim numbers As String() = row.Cells(2).Value.ToString().Split("-"c)
If numbers.Length > 1 Then
row.Cells(2).Value = row.Cells(2).Value.ToString().Split("-"c)(1)
End If
End If
Next
End Sub
End Class
Screenshot