简体   繁体   English

如何将 MySQL 中的记录设置到 datagridview 的列中?

[英]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.我想在 Visual Studio 2010 中将数据库(MySQL)中的记录转换为 DataGridView 的 Header。

I want to set my datagridview like this: https://imgur.com/jVqhnFJ我想这样设置我的datagridview: 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.没有解释什么是“26”和“27”列。

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.请注意,无论您在结果集中想要什么列, UNION前半部分中的列必须与第二部分中的列匹配。 That is why I这就是为什么我

You want also to visually present the Total row, ie:您还希望直观地呈现Total行,即:

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.请注意,为了清楚起见,此示例使用 sql 服务器的TOP 5将结果集限制为 5+1 行(在 MySQL 中它有点不同,附加LIMIT 5 )。 This is not wanted in real scenario.这在实际场景中是不需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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