如果由CheckboxColumn检查,则保存Datagridview

时间:2016-05-25 05:48:10

标签: mysql vb.net

大家好,下午好,

我在表单中有一个对象,它们是FILE LOCKED WITH ONLY READERS而保存Datagridview1 Button将从我的数据库Datagridview1填充数据,数据将显示为一个相应的复选框。如下面的图片

enter image description here

如果你在这里是

的代码
Form_Load

我的问题是如何在数据库中保存Checked Row?让我说我检查了所有这些,所以行将保存在数据库中。 (无论我检查了多少)

这是我的代码但不起作用。 :(

 Private Sub loadtech()
        Dim con1 As MySqlConnection = New MySqlConnection("datasource=localhost;database=operations;userid=root;password=admin1950;Convert Zero Datetime=True")
        Dim sql1 As MySqlCommand = New MySqlCommand("select TechName from technicians order by Category ASC", con1)
        Dim ds1 As DataSet = New DataSet
        Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
        con1.Open()
        adapter1.SelectCommand = sql1
        adapter1.Fill(ds1, "MyTable")
        DataGridView1.DataSource = ds1.Tables(0)
        con1.Close()

        With DataGridView1
            .RowHeadersVisible = False
            .Columns(0).HeaderCell.Value = "Technician / Electrician"
        End With
        DataGridView1.Columns.Item(0).Width = 150
        DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
        Me.DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
        Me.DataGridView1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
        checkBoxColumn.HeaderText = "Tag"
        checkBoxColumn.Width = 30
        checkBoxColumn.Name = "checkBoxColumn"
        DataGridView1.Columns.Insert(0, checkBoxColumn)
    End Sub

TYSM未来的帮助

1 个答案:

答案 0 :(得分:0)

是的,你的循环略有不正确。尝试使用此循环,看看是否可以解决您的问题。问题是,您没有使用i变量。它应放在Row(i)中,当它从0到Count时,你从0循环到Count <1

Dim name As String
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1 Step 1
           If Me.DataGridView1.Rows(i).Cells(0).Value = True Then
            name = Me.DataGridView1.Rows(i).Cells(1).Value


            comm.CommandText = "insert into assignments(ElecAssigned) values('" & name & "')"
            comm.ExecuteNonQuery()
           End If
        Next