Solved

Button to delete rows from a DataGridView

Posted on 2006-11-09
7
4,175 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

627 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