Solved

Button to delete rows from a DataGridView

Posted on 2006-11-09
7
4,167 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error "String or binary data would be truncated" in an update on a datatable 7 35
VS.net 2010 11 32
Crystal reports vb.net 2 39
SQL LINE CONTINUATION ISSUE 12 31
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…
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 …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 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

12 Experts available now in Live!

Get 1:1 Help Now