Solved

Button to delete rows from a DataGridView

Posted on 2006-11-09
7
4,169 Views
Last Modified: 2011-08-18
I have a datagridview which is bound to the database with the following code:

    Public Sub RefreshForm()
        Dim i As Byte
        'For i = 1 To dgvTeachers.Rows.Count
        '    dgvTeachers.Rows.RemoveAt(0)
        'Next
        frmMain.connWorkshop.Open()
        Dim strSQL As String = "select ID, FName ,LName from tblTeachers"
        Dim daTeachers As New SqlDataAdapter(strSQL, frmMain.connWorkshop)
        daTeachers.TableMappings.Add("Table", "tblTeachers")
        dsTeachers.Reset()
        daTeachers.Fill(dsTeachers)
        With dgvTeachers
            .DataSource = dsTeachers.Tables("tblTeachers")
            .Columns(0).ReadOnly = True
            .Columns(0).HeaderText = ""
            .Columns(0).Width = 30
            .Columns(1).HeaderText = "Name"
            .Columns(2).HeaderText = "Surname"
            .Refresh()
        End With
        frmMain.connWorkshop.Close()
    End Sub

    Private Sub dgvTeachers_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTeachers.CellEndEdit
        BindingContext(dsTeachers.Tables("tblTeachers")).EndCurrentEdit()
        frmMain.connWorkshop.Open()
        Dim strSQL As String = "select ID, FName ,LName from tblTeachers"
        Dim daTeachers As New SqlDataAdapter(strSQL, frmMain.connWorkshop)
        daTeachers.TableMappings.Add("Table", "tblTeachers")
        Dim command_builder As New SqlCommandBuilder(daTeachers)
        daTeachers.ContinueUpdateOnError = False
        daTeachers.Update(dsTeachers)
        frmMain.connWorkshop.Close()
        Me.RefreshForm()
    End Sub

I have disabled deleting rows in that datagridview. Instead, I have added a button to the below, named "Delete". When it is pressed, it should show an Are You Sure dialog, and if Yes is pressed, it should delete the selected row from datagridview. (Also, it must prevent deleting more than one row at a time.)

How can I code this button's click handler?
0
Comment
Question by:huji
7 Comments
 
LVL 39

Assisted Solution

by:appari
appari earned 50 total points
ID: 17912469
try like this

if dgvTeachers.SelectedRows.count>0 then
       Dim selRow As DataGridViewRow = dgvTeachers.SelectedRows(0)
         dgvTeachers.Rows.Remove(selRow)
endif
0
 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17912540
Hi huji,

Try:

if (dgvTeachers.SelectedRows.count = 1) then
  Dim answer As DialogResult = MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
  If answer = DialogResult.Yes Then
    Dim selRow As DataGridViewRow = dgvTeachers.SelectedRows(0)
    dgvTeachers.Rows.Remove(selRow)
  End If  
else if (dgvTeachers.SelectedRows.count > 1) then
    Messagebox.show("too many rows selected")
else
    Messagebox.show("no rows selected")
end if

Cheers!
0
 
LVL 14

Author Comment

by:huji
ID: 17912580
Every thing is fine, the only thing is, I have hidden the RowHeaders in the DGV. The user can focus to a cell in the DGV, and when the Delete button is pressed, I want the code to automatically detect the row, in which the user currently is. (In breif, I want the code to interpret selecting a cell, as selecting the whole row.) Any idea?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Hillwaaa
ID: 17912626
Hey - the following url has an example of how to do this:

http://www.tek-tips.com/faqs.cfm?fid=3712

Cheers,
Hillwaaa.
0
 
LVL 14

Author Comment

by:huji
ID: 17915247
I'm using a datagridview not a datagrid. I hope there is a better way than controlling user clicks.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17915906
You could add a button column to the grid, with "delete" on it... or is this too simple an approach?
0
 
LVL 16

Accepted Solution

by:
Hillwaaa earned 200 total points
ID: 17918765
Hi huji,

Use the CurrentCell function:

if Not dgvTeachers.CurrentCell is nothing then
    Dim selRow As DataGridViewRow = dgvTeachers.Rows(dgvTeachers.CurrentCell.RowIndex)
    dgvTeachers.Rows.Remove(selRow)
End If  

For more info: http://msdn2.microsoft.com/en-us/library/yc4fsbf5.aspx

Cheers,
Hillwaaa
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question