简体   繁体   中英

How to set record in MySQL into column in datagridview?

I want to convert records in the database (MySQL) to DataGridView's Header in Visual Studio 2010.

I want to set my datagridview like this: https://imgur.com/jVqhnFJ

This is my script:

Private Sub show_data()
        conn.Open()
        Dim strDateTime = Today.Month
        Dim cmdselect = New MySqlCommand("SELECT * FROM sell WHERE month(date)='"& strDateTime &"'", conn)
        Dim rd = cmdselect.ExecuteReader

        If rd.HasRows Then
            Try
                conn.Close()
                conn.Open()

                da = New MySqlDataAdapter("SELECT * FROM sell WHERE month(date)='" & strDateTime & "'", conn)

                'the code maybe goes here..
                (.....)

                dt.Clear()
                da.Fill(dt)
                DGV.DataSource = dt
                Me.DGV.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                conn.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            MsgBox("Failed")
        End If

        DGV.ReadOnly = True

    End Sub

Thank you so much for your helping. (sorry for my bad English).

There's no explanation what's "26" and "27" column.

Other then that, one way to do it is:

SELECT Description, Total26, Total27 FROM Sell

UNION ALL

SELECT 'Total' as Description, SUM(Total26) as Total26, SUM(Total27) as Total27 FROM Sell

Note, that whatever columns you wamt in the result set, the columns in the first half of the UNION must match the columns in the second part. That is why I

You want also to visually present the Total row, ie:

Me.DataGridView1.Rows(Me.DataGridView1.Rows.Count-1).DefaultCellStyle.BackColor = Color.Navy;
Me.DataGridView1.Rows(Me.DataGridView1.Rows.Count-1).DefaultCellStyle.ForeColor = Color.White;

EXAMPLE

  SELECT TOP 5 CAST(BinDateTime as nvarchar(10)) as TestPeriod, Duration FROM Test_BinItems 
  UNION ALL
  SELECT 'TOTAL' as TestPeriod, SUM(Duration) FROM Test_BinItems 

联合示例

Note, that this example is using sql server's TOP 5 to limit result set to 5+1 rows (in MySQL it's a bit different, appending LIMIT 5 ) for clarity. This is not wanted in real scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM