I'm trying to use CellValueChanged but why doesn't CellValueChanged immediately appear in the expression datatable column (MATERIALCOLORNAME) in vb.net
I have the code below
please guide me
Thanks
Private Sub CreateDataTable()
If m_DataTable IsNot Nothing Then Return
m_DataTable = New DataTable
'Columns
m_DataTable.Columns.Add("CODEPRODUCT", GetType(String))
m_DataTable.Columns.Add("DESCRIPTION", GetType(String))
m_DataTable.Columns.Add("COLORCODE", GetType(String))
m_DataTable.Columns.Add("COLORNAME", GetType(String))
m_DataTable.Columns.Add("MATERIAL", GetType(String))
m_DataTable.Columns.Add("MATERIALCOLORNAME", GetType(String), "TRIM(IIF(MATERIAL Is Null Or SUBSTRING(MATERIAL, 1, 1) = '-', '', MATERIAL) + IIF(COLORNAME Is Null Or SUBSTRING(COLORNAME, 1, 1) = '-', '', ' ' + COLORNAME))")
End Sub
this is the code I use :
Public m_DataTable As DataTable
Public Function GetConnectionString2() As String
Dim strCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\TRIAL.accdb;Persist Security Info=False;"
Return strCon
End Function
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.RowIndex < 0 OrElse e.RowIndex < 0 Then Return
Dim dgv = DirectCast(sender, DataGridView)
'If Not dgv.IsCurrentCellDirty Then Return
Dim row = dgv.Rows(e.RowIndex)
Dim arrQueries() = {
New With {
.KeyCell = row.Cells("CODEPRODUCT"),
.ValueCell = row.Cells("DESCRIPTION"),
.Table = "PRODUCT"},
New With {
.KeyCell = row.Cells("SIZEPRODUCT"),
.ValueCell = row.Cells("CATEGORY"),
.Table = "SIZEPRODUCT"},
New With {
.KeyCell = row.Cells("COLORCODE"),
.ValueCell = row.Cells("COLORNAME"),
.Table = "COLORCODE"},
New With {
.KeyCell = row.Cells("COLORCODE"),
.ValueCell = row.Cells("MATERIAL"),
.Table = "COLORCODE"}
}
If Not arrQueries.Any(
Function(q) q.KeyCell.OwningColumn Is dgv.Columns(e.ColumnIndex)) Then
Return
End If
Using con = New OleDbConnection(GetConnectionString2()), cmd = con.CreateCommand()
con.Open()
For Each q In arrQueries
If q.KeyCell.Value IsNot Nothing AndAlso
q.KeyCell.Value IsNot DBNull.Value AndAlso
Not String.IsNullOrEmpty(q.KeyCell.Value.ToString()) Then
cmd.CommandText = String.Format(
"SELECT {0} FROM {1} WHERE {2} = ?",
q.ValueCell.OwningColumn.Name, q.Table, q.KeyCell.OwningColumn.Name)
cmd.Parameters.Clear()
cmd.Parameters.Add("?", OleDbType.VarWChar).Value = q.KeyCell.Value
q.ValueCell.Value = cmd.ExecuteScalar()
Else
q.ValueCell.Value = Nothing
End If
Next
End Using
End Sub
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateDataTable()
DataGridView1.DataSource = m_DataTable
End Sub
this is a gif of the cellvaluechanged process