Solved

Button to delete rows from a DataGridView

Posted on 2006-11-09
7
4,160 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now