Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Rows from a DataTable

Status
Not open for further replies.

DH

Programmer
Dec 8, 2000
168
0
0
Hello,

I am using the following code to create a dataTable:

Code:
    Shared Function GetDataTable() As DataTable

        Dim da As SqlDataAdapter
        Dim ds As New DataSet
        Dim sConn As String = ConfigurationSettings.AppSettings("SqlConnection1.ConnectionString")
        Dim oConn As New SqlConnection(sConn)
        Dim sqlcmd As New SqlCommand

        'Prepare command object
        sqlcmd.Connection = oConn
        sqlcmd.CommandType = CommandType.StoredProcedure
        sqlcmd.Parameters.Add("@allIDs", session("IDs")
        sqlcmd.CommandText = "Read_Emails"

        da = New SqlDataAdapter(sqlcmd)
        da.Fill(ds, "tblMailMerge")
        Dim dtMailMerge As DataTable = ds.Tables(0)
        Return dtMailMerge

    End Function

Once the datatable is populated, I allow users to select which IDs the user would acutally like to email. I retreive the selected ID's as an array (or comma-delimited string).

I would like to delete the rows in the original datatable that are not in the array (or comma-delimited string)

Does anyone have any suggestions or examples.

Thanks,

DH
 
You just have to find the index of the rows that you want to delete and then call the Delete method of the relevant row e.g.
Code:
dt.Rows(0).Delete


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I'm having a little trouble finding the index of a row when I only know the value of a field. For example, I have a column named 'invoiceNumber' with a value '12345'

DH
 
How are you displaying these values to the users? How do they select which tows they want to email?
 
I display the rows in a datagrid, each row of the datagrid has a checkbox which allows the user to select a row. I then loop through the datagrid rows to grab the ids for the selected rows.

 
Well, if the row has a check, then you would grab the index on that row to keep. However, as you loop through your rows, if there isn't a check, you could delete that row.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top