Solved

Button to delete rows from a DataGridView

Posted on 2006-11-09
7
4,171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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