如何在选择vb.net中选择复选框时使用DataGridView发送多个消息



这是我的代码:

'for clicking the datagridview
Private Sub DataGridView1_CellContentClick_1(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Try
            MySqlConnection.ConnectionString = "Server=localhost; User=root;Password='';Database=lrbhams;"

            MySqlConnection.Open()
            Dim state3 As String = "select boarder_fname, boarder_contact, guardian, guardian_number from boarders_info"
            Dim command As New MySqlCommand(state3, MySqlConnection)
            'command.Connection.Open()
            command.ExecuteNonQuery()
            MySqlConnection.Close()
            If DataGridView1.Rows(e.RowIndex).Cells(2).Value Then
                TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value
            ElseIf DataGridView1.Rows(e.RowIndex).Cells(4).Value Then
                TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(4).Value
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        ''
    End Sub

发送消息:

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim message As String
        'Dim reader As MySqlDataReader

        message = RichTextBox1.Text
    If send_sms.SerialPort.PortName = send_sms.portName Then
        send_sms.SerialPort.Write("AT" & vbCrLf)
        send_sms.SerialPort.Write("AT+CMGF=1" & vbCrLf)
        send_sms.SerialPort.Write("AT+CMGS=" & Chr(34) & TextBox1.Text & Chr(34) & vbCrLf)
        send_sms.SerialPort.Write(message & Chr(26))
        MsgBox("Text Message Successfully Send !!!")
        Try
            Dim connectString As String
            Dim conn As New MySqlConnection
            Dim reader As MySqlDataReader
            Dim command As MySqlCommand
            'Dim mysqlQuery As String
            connectString = "server=localhost;user=root;database=lrbhams"
            conn = New MySqlConnection(connectString)
            Dim query As String
            conn.Open()
            query = "INSERT into admin_log_attendance (action,contact_number, date) VALUES ('" & RichTextBox1.Text & "','" & TextBox1.Text & "', '" & Date.Today & "')"
            command = New MySqlCommand(query, conn)
            reader = Command.ExecuteReader
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End If
End Sub

MySqlConnectionMySql.Data.MySqlClien T名称空间中的类。它不是静态类,因此必须与新关键字进行实例化。您无法在类本身上设置属性或调用方法。您必须创建类的实例。使用`使用...使用块用于数据库对象是最好的做法。它将确保即使存在错误,数据库对象也会关闭和处理。

Select语句不是非Query。插入,更新和删除语句可以使用command.ExecuteNonQuery,但不能运行Select。如果预期一块数据,则可以使用.ExecuteScalar查询CC_7,如果有几列和/或行,则可以使用CC_7。

您不能仅仅执行Select命令并期望发生某些事情。我怀疑您想在网格中显示数据,所以让我们用.Load方法填充DataTable(记录的记忆表示),然后将其绑定到DataGridView

无需关闭连接,因为使用块将关闭。

如果您在不覆盖方法的类上调用.ToString,则只需获得全班的完全限定名称即可。Exception类提供了一个称为MessageProperty,您可以使用。

DataGridView的填充将以单独的方法进行,您可以从Form.Load调用。

现在,DataGridView中有数据,您可以单击它,CellContentClick事件将触发。我希望单元格(2)和单元格(4)布尔值,因为这是您的If语句会起作用的唯一原因。一旦您打开选项严格,转换代码和.ToString的必要性将是显而易见的。(请参阅我的评论)

现在就足够了。使用所有这些和选项严格,您应该能够修复代码的第二部分。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    RetrieveRecordsForDataGridView()
End Sub
Private Sub RetrieveRecordsForDataGridView()
    Dim dt As New DataTable
    Try
        Using cn As New MySqlConnection("Server=localhost; User=root;Password='';Database=lrbhams;")
            Dim state3 As String = "select boarder_fname, boarder_contact, guardian, guardian_number from boarders_info"
            Using command As New MySqlCommand(state3, cn)
                cn.Open()
                dt.Load(command.ExecuteReader())
            End Using
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If CBool(DataGridView1.Rows(e.RowIndex).Cells(2).Value) Then
        TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString
    ElseIf CBool(DataGridView1.Rows(e.RowIndex).Cells(4).Value) Then
        TextBox1.Text = DataGridView1.Rows(e.RowIndex).Cells(4).Value.ToString
    End If
End Sub

最新更新