简体   繁体   English

vb.net 索引所选单元格的行

[英]vb.net indexing the row of the selected cell

i showing my table in a data grid then i want to update this table from the grid itself i am using this code but this error keeps showing The class 'System.Windows.Forms.DataGridViewRow' cannot be indexed because it has no default property.我在数据网格中显示我的表然后我想从网格本身更新这个表我正在使用这个代码但是这个错误一直显示类'System.Windows.Forms.DataGridViewRow'不能被索引,因为它没有默认属性。

Private Sub edit()
    Dim cnx As New MySqlConnection("datasource=localhost;database=bdgeststock;username=root;password=")
    Dim cmd As MySqlCommand = cnx.CreateCommand
    Dim resultat As Integer
    Dim req As String = "UPDATE utilisateur  SET @col = @val where idu=@id"
    If grid.CurrentCell.ColumnIndex = 1 Then
        MessageBox.Show("error u cant edit ids")
    Else
        If ConnectionState.Open Then
            cnx.Close()
        End If
        cnx.Open()
        cmd.Parameters.AddWithValue("@col", grid.CurrentCell.ColumnIndex)
        cmd.Parameters.AddWithValue("@val", grid.CurrentCell.Value)
        cmd.Parameters.AddWithValue("@id", grid.CurrentRow(0))
        cmd.CommandText = req
        resultat = cmd.ExecuteNonQuery
        If (resultat = 0) Then
            MessageBox.Show("error")
        Else
            MessageBox.Show("success")
        End If
        grid.EndEdit()
        grid.RefreshEdit()
        grid.ReadOnly = True
        cnx.Close()
        cmd.Dispose()
    End If

End Sub

Calling grid.CurrentRow(0) won't work.调用grid.CurrentRow(0)将不起作用。 grid.CurrentRow returns a datagridviewrow, but as the errror message says, this cannot be indexed (means "cannot have a number in brackets put after it") because it has no default property (means the property that is returned when the developer uses just the name). grid.CurrentRow返回一个datagridviewrow,但正如错误消息所说,这不能被索引(意思是“后面不能有括号中的数字”),因为它没有默认属性(意思是开发人员使用时返回的属性)名字)。 Some things do have default properties;有些东西确实有默认属性; for example a DataRow (of a datatable) has a .Items property representing the values of the columns.例如,(数据表的)DataRow 有一个.Items属性,表示列的值。 .Items is a default property meaning that myDatarow.Items(7) and myDataRow(7) do exactly the same thing. .Items是一个默认属性,这意味着myDatarow.Items(7)myDataRow(7)做的事情完全一样。 DataGridViewRow doesn't have any of its properties marked as a default one, so you have to specify one of its properties DataGridViewRow 没有任何属性标记为默认属性,因此您必须指定其属性之一

Perhaps you meant也许你的意思是

grid.CurrentRow.Cells(0).Value

Now might be a good time to mention that this is isn't the way MS intended a datagridview be used.现在可能是提到这不是 MS 打算使用 datagridview 的方式的好时机。 It would be better to bind your grid to a datatable, and then pull the data items you want out of the datatable.最好将您的网格绑定到数据表,然后从数据表中提取您想要的数据项。

You might also, if your grid is bound to a datatable:如果您的网格绑定到数据表,您也可以:

DirectCast(grid.CurrentRow.DataBoundItem, DataRowView)(0)

I would recommend ensuring that you don't allow the user to change the order of columns in the DGV.我建议确保您不允许用户更改 DGV 中列的顺序。 If you do, then consider one of:如果您这样做,请考虑以下之一:

grid.CurrentRow.Cells("id").Value

DirectCast(grid.CurrentRow.DataBoundItem, DataRowView)("id")

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

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