Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using vb.net to connect to mysql database and I parse the data to a datagridview and then I do some calculations and I create 2 extra columns to hold the calculations data. People told me that this is not a good practice so instead I must do the calculations in the datatable and then parse the data to datagridview. I searched for solutions and tried different stuff but I can't make it work. Does anyone knows how can I do that?

Here Is the code I am using so far.

VB.NET
<pre>     Imports MySql.Data.MySqlClient
        
     Public Class Form1
        
         Dim MysqlConn As MySqlConnection
        
         Dim COMMAND As MySqlCommand
        
        
         Public sconnection As New MySqlConnection
        
         Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
             'open the connection to mysql
        
              If sconnection.State = ConnectionState.Closed Then
                 sconnection.ConnectionString = "server=sql11.freesqldatabase.com;userid=sql11508072;password=s2PWmyaDkE"
                 sconnection.Open()
        
             End If
        
         End Sub
        
        
        
         Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        
             'Connect to mysql to get data
             MysqlConn = New MySqlConnection
             MysqlConn.ConnectionString = "server=sql11.freesqldatabase.com;userid=sql11508072;password=s2PWmyaDkE"
        
             Dim SDA As New MySqlDataAdapter
             Dim dbdataset As New DataTable
             Dim bsource As New BindingSource
        
             'Clear datagridview before re-apply data
             dbdataset.Clear()
             DataGridView1.Columns.Clear()
             DataGridView1.Refresh()
        
             Try
                 MysqlConn.Open()
                 Dim query As String
                 query = ("select * from sql11508072.Complete ")
                 COMMAND = New MySqlCommand(query, MysqlConn)
                 SDA.SelectCommand = COMMAND
        
                 SDA.Fill(dbdataset)
                 bsource.DataSource = dbdataset
                 DataGridView1.DataSource = bsource
                 SDA.Update(dbdataset)
        
                 MysqlConn.Close()
        
             Catch ex As Exception
                 MessageBox.Show(ex.Message)
             Finally
                 MysqlConn.Dispose()
        
             End Try
        
             'Add additional columns to datagridview
             DataGridView1.Columns.Add("NameOfColumn", "First Calc Test")
             DataGridView1.Columns.Add("NameOfColumn", "Second Calc Test")
        
        
             'Do some test calculations
             Try
                 For i As Integer = DataGridView1.RowCount - 1 - 1 To 0 Step -1
                     If CInt(Replace(DataGridView1.Rows(i).Cells(5).Value, ",", "")) > 2 Then
                         Me.DataGridView1.Rows(i).Cells(8).Value = "High"
                     ElseIf CInt(Replace(DataGridView1.Rows(i).Cells(5).Value, ",", "")) > 1 Then
                         Me.DataGridView1.Rows(i).Cells(8).Value = "Medium"
                     ElseIf CInt(Replace(DataGridView1.Rows(i).Cells(5).Value, ",", "")) > 0 Then
                         Me.DataGridView1.Rows(i).Cells(8).Value = "Low"
                     ElseIf CInt(Replace(DataGridView1.Rows(i).Cells(5).Value, ",", "")) < 1 Then
                         Me.DataGridView1.Rows(i).Cells(8).Value = "Very Low"
                     End If
        
                     If CInt(Replace(DataGridView1.Rows(i).Cells(7).Value, ",", "")) - CInt(Replace(DataGridView1.Rows(i).Cells(5).Value, ",", "")) > 2 Then
                         Me.DataGridView1.Rows(i).Cells(9).Value = "High"
                     ElseIf CInt(Replace(DataGridView1.Rows(i).Cells(7).Value, ",", "")) - CInt(Replace(DataGridView1.Rows(i).Cells(5).Value, ",", "")) > 1 Then
                         Me.DataGridView1.Rows(i).Cells(9).Value = "Medium"
                     ElseIf CInt(Replace(DataGridView1.Rows(i).Cells(7).Value, ",", "")) - CInt(Replace(DataGridView1.Rows(i).Cells(5).Value, ",", "")) > 0 Then
                         Me.DataGridView1.Rows(i).Cells(9).Value = "Low"
                     ElseIf CInt(Replace(DataGridView1.Rows(i).Cells(7).Value, ",", "")) - CInt(Replace(DataGridView1.Rows(i).Cells(5).Value, ",", "")) < 1 Then
                         Me.DataGridView1.Rows(i).Cells(9).Value = "Very Low"
                     End If
        
        
                 Next
             Catch
             End Try
        
         End Sub
        
        
     End Class


What I have tried:

I tried some different code but I don't have anything that is usable.
Posted
Updated 24-Jul-22 1:07am

1 solution

Why not do it as part of your SQL?
Instead of
SQL
SELECT * FROM MyTable
Generate your columns there:
SQL
SELECT Qty, Price, Qty * Price AS [Line Value] FROM MyTable
Gives you three columns in your DT:
SQL
Qty    Price    Line Value
  1    16.66         16.66
  5    20.00        100.00
Or even use a Computed Columns in a Table - SQL Server[^] to "hide" the math behind the scenes in the DB?
 
Share this answer
 
Comments
nikosthe15 24-Jul-22 11:03am    
I am using mysql and people told me that is not recommended to do it on a server side (online server).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900