我在使用datagrid时用了DataGrid1.CurrentRowIndex来获取索引,但只能是每次删除一行数据,
'删除一行数据,代码如下,那如何同时删除多行数据?请各位高手指点以下,在下十分感激!
Dim i As Integer = DataGrid1.CurrentRowIndex '定义I是datagrid的被选中的一行
If i >= 0 Then
'当I存在时,定义V是DATAVIEW,利用DATAVIEW的DELETE方法删除一行数据
If MsgBox("是否删除选定项?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Dim v As DataView
v = New DataView(DataSet12.Tables("sh1"))
DataGrid1.DataSource = v v.Delete(i)
Try
OleDbDataAdapter1.Update(DataSet12, "sh1") DataSet12.Clear() OleDbDataAdapter1.Fill(DataSet12, "sh1") Catch
End Try OleDbConnection1.Close()
End If End If
'删除一行数据,代码如下,那如何同时删除多行数据?请各位高手指点以下,在下十分感激!
Dim i As Integer = DataGrid1.CurrentRowIndex '定义I是datagrid的被选中的一行
If i >= 0 Then
'当I存在时,定义V是DATAVIEW,利用DATAVIEW的DELETE方法删除一行数据
If MsgBox("是否删除选定项?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Dim v As DataView
v = New DataView(DataSet12.Tables("sh1"))
DataGrid1.DataSource = v v.Delete(i)
Try
OleDbDataAdapter1.Update(DataSet12, "sh1") DataSet12.Clear() OleDbDataAdapter1.Fill(DataSet12, "sh1") Catch
End Try OleDbConnection1.Close()
End If End If
Dim varBmk As Variant
For Each varBmk In DataGrid1.SelBooks
ADODC1.Recordset.Book = varBmk
ADODC1.Recordset.Delete
ADODC1.Refresh
Next
End Sub
Dim appdisk$, trecord%, i&, j&, k&, t&, Tmprnd&(), TselRec&
Private Sub Form_Load()
appdisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
Command1.Caption = "删 除"
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & appdisk & "bwscale.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from bwmain" 'where inday =" & Chr(35) & Text2.Text & Chr(35)
Set DataGrid1.DataSource = Adodc1
trecord = Adodc1.Recordset.RecordCount
If trecord > 0 Then Adodc1.Recordset.MoveFirst
End SubPrivate Sub Command1_Click()
TselRec = DataGrid1.SelBooks.Count
ReDim Preserve Tmprnd&(TselRec)
For i = 0 To TselRec - 1
Tmprnd&(i) = DataGrid1.SelBooks.Item(i)
Next i
If TselRec > 1 Then
'*********** 排序数组变量
For i = 0 To TselRec - 1
k = i
For j = i + 1 To TselRec - 1
If Tmprnd(k) < Tmprnd(j) Then k = j
Next j
t = Tmprnd(i): Tmprnd(i) = Tmprnd(k): Tmprnd(k) = t
Next i
End If
For i = 0 To TselRec - 1
If Adodc1.Recordset.RecordCount <= 0 Then Exit For
Adodc1.Recordset.MoveFirst
If Tmprnd(i) - 1 > 0 Then Adodc1.Recordset.Move Tmprnd(i) - 1
Adodc1.Recordset.Delete
Next i
If Adodc1.Recordset.RecordCount > 0 Then Adodc1.Recordset.MoveFirst
Set DataGrid1.DataSource = Nothing
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
End Sub