Subject: DataGrid Information
Posted By: KennethMungwira Post Date: 1/27/2004 3:57:59 PM
Dear Users,

i am trying to delete multiple records in a datagrid, at once. How is this done, please look at the code I have which deletes one record at a time....Only the record that is currently highlight, is deleted, can I highlight mulitple rows and delete all of them at the same time?

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim IRow As Integer
        Dim TRow As Integer
        Dim LRow As String
        Dim MyRow As DataRow
        Dim NTID As String
        ProgressBar1.Visible = False
        NTID = txtNTID.Text


        IRow = dgdResults.Item(dgdResults.CurrentCell)
        TRow = dgdResults.CurrentCell.RowNumber

        Dim strSQL As String = "SELECT [ID],[ItemName], [StartDate], [EndDate], [StartTime], [StopTime], [NTID] From (Requests)"
        strSQL = strSQL & " Where(([NTID]='" & NTID & "'));"
        Dim adodbConnection As New ADODB.Connection()

        adodbConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=C:\DATA\ACCESS\13FloorEquipment.mdb;"

        adodbConnection.Open()

        Dim rs As ADODB.Recordset = New ADODB.Recordset()
        Dim adoRS As Recordset = New Recordset()
        adoRS.Open(strSQL, adodbConnection, CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockOptimistic, 0)
        'adoRS.Open(strSQL, adodbConnection, CursorTypeEnum.adOpenKeyset, LockTypeEnum.adLockBatchOptimistic, 0)

        If adoRS.EOF Then
            MsgBox("There are no records")
        Else
            If adoRS.RecordCount <> 0 Then
                adoRS.MoveFirst()
            End If
        End If
        Do Until adoRS.EOF
            If adoRS.Fields("ID").Value = IRow Then
                adoRS.Delete()
                GoTo Quit
            Else
                adoRS.MoveNext()
                ProgressBar1.PerformStep()
            End If
        Loop
Quit:
        MsgBox("Record is Deleted")
        adoRS.Close()
Reply By: Imar Reply Date: 1/27/2004 4:30:15 PM
Hi there,

I think the question has been asked before, but you never answered it: Why on earth are you using ADO and other Legacy code? Goto was banned 5 years ago ;-) IMO, by coding this way, you're not using .NET to the fullest. In fact, you may even make it harder on your self than strictly necessary.

Anyway, there are a few ways to select rows in a grid and then check which rows are selected. One way is to use the Select method of the Grid. Pass it an index of the row you want to select, to select that row.

Then you can use IsSelected(rowIndex) to determine which rows are selected. Loop through all the rows in the Grid and see whether they are selected or not.

I am sure there are better ways (I am not really into the DataGrid for Windows forms), but this should at least work.

However, before you continue, I would strongly advice to "upgrade" your code to .NET. Programming classic VB and ADO in .NET is going to get you into real problems soon......

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply By: KennethMungwira Reply Date: 1/28/2004 10:38:13 AM
Dear Programmers,

One reason for me using ADO.net, is due to the fact the Database is on my company network, and is not on an SQL server..... Should that make a difference
Reply By: Imar Reply Date: 1/28/2004 10:44:33 AM
You're not using ADO.NET, you're using "classic" ADO. ADO.NET is completely different from ADO. Instead of creating a Recordset, you would create a DataSet or an OleDbDataReader to access your Access database in .NET.

Take a look here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbDataReaderClassTopic.asp for more info about the DataReader and here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataOleDbOleDbDataReaderClassTopic.asp for more info about the DataSet.

I suggest you Google a bit for ADO.NET, DataSet and Tutorial. There are tons of sites out on the Web that'll teach you how to code stuff like this in "proper" .NET.

And thanks for your personal message. Would you mind posting your queries here instead of directly sending them to me? Makes things a lot easier....

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply By: devendra_sahumk Reply Date: 4/8/2004 6:57:47 AM
hi i want to know how i can add four records of database
table in one row of datagrid.and each record have more than one fields in database
Reply By: sachinmahajan Reply Date: 9/24/2004 7:41:50 AM
Imar, If i select multiple row in the datagrid and then i sort the Grid,and then  i wanna to delete all the selected record,But when i click on the delete button then when i loop the grid and check the IsSeleted and then delete the row then it delete only one record,
Can you suggest me this is the bug with the DataGrid.

Reply By: Imar Reply Date: 9/24/2004 10:23:08 AM
Hi sachinmahajan,

Can you please start a new thread when you have a different topic? That makes things much easier to track.

That said, I don't believe the behavior is being caused by a bug in the datagrid. Maybe it's caused by a misunderstanding of how the DataGrid works, or maybe something else is wrong.

I don't fully understand the situation (why would you sort the data after you have checked some items and are about to delete them), so can you:

a) Start a new thread for this topic (just click the New Topic link)

and

b) describe your situation in more detail. Posting the relevant parts of your code surely helps explaining the problem.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Sour Times by Portishead (Track 2 from the album: Dummy) What's This?

Go to topic 19576

Return to index page 765
Return to index page 764
Return to index page 763
Return to index page 762
Return to index page 761
Return to index page 760
Return to index page 759
Return to index page 758
Return to index page 757
Return to index page 756