[英]vb.net indexing the row of the selected cell
我在數據網格中顯示我的表然后我想從網格本身更新這個表我正在使用這個代碼但是這個錯誤一直顯示類'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
調用grid.CurrentRow(0)
將不起作用。 grid.CurrentRow
返回一個datagridviewrow,但正如錯誤消息所說,這不能被索引(意思是“后面不能有括號中的數字”),因為它沒有默認屬性(意思是開發人員使用時返回的屬性)名字)。 有些東西確實有默認屬性; 例如,(數據表的)DataRow 有一個.Items
屬性,表示列的值。 .Items
是一個默認屬性,這意味着myDatarow.Items(7)
和myDataRow(7)
做的事情完全一樣。 DataGridViewRow 沒有任何屬性標記為默認屬性,因此您必須指定其屬性之一
也許你的意思是
grid.CurrentRow.Cells(0).Value
現在可能是提到這不是 MS 打算使用 datagridview 的方式的好時機。 最好將您的網格綁定到數據表,然后從數據表中提取您想要的數據項。
如果您的網格綁定到數據表,您也可以:
DirectCast(grid.CurrentRow.DataBoundItem, DataRowView)(0)
我建議確保您不允許用戶更改 DGV 中列的順序。 如果您這樣做,請考慮以下之一:
grid.CurrentRow.Cells("id").Value
DirectCast(grid.CurrentRow.DataBoundItem, DataRowView)("id")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.