?
Solved

Delete selected row on right click in vb.net

Posted on 2014-02-24
4
Medium Priority
?
648 Views
Last Modified: 2014-02-24
Hi all.

I have a datagridview control that displays records based on the code below in Visual Studio 2010. I want to be able to right click a row and when the end user selects DELETE then it deletes it from the table and the datagridview control is refreshed. As you can see, I select the QuoteNumber which is the primary key of the table, but I don't show it in the datagridview control (I don't know if this will cause an issue). I started building the ContextMenuStrip1_Click event and I get an error at the cmd.ExecuteNonQuery() line: "No mapping exists from the object type System.Windows.Form.DataGridViewTextBoxColumn to a known managed provider native type"

Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = "Data Source=myserver;Initial Catalog=mydb;Integrated Security=True"

        con.Open()

        cmd.Connection = con

        cmd.CommandText = "SELECT  IQ.QuoteNumber,IQ.MasterItemNumber, IQ.Description, IQ.SLXID, SLX.ACCOUNT,IQ.QuoteDate, IQ.UnitCost, IQ.QuotedCost, IQ.MASItemNumber, IQ.Notes FROM mydb.dbo.IQuote IQ INNER JOIN [mydb2].db2.sysdba.ACCOUNT SLX ON IQ.SLXID = SLX.ACCOUNTID WHERE MasterItemNumber = @MasterItemNo"

        cmd.Parameters.AddWithValue("@MasterItemNo", txtMasterItemNo.Text)

        Dim sda As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()

        DataGridView1.DataSource = Nothing

        sda.Fill(dt)

DataGridView1.ColumnCount = 3   'Set Columns Count

        'Add Columns

        DataGridView1.Columns(0).Name = "MasterItemNumber"
        DataGridView1.Columns(0).HeaderText = "Master Item Number"
        DataGridView1.Columns(0).DataPropertyName = "MasterItemNumber"
        DataGridView1.Columns(0).Width = 180

        DataGridView1.Columns(1).Name = "MyItemNumber"
        DataGridView1.Columns(1).HeaderText = "My Item Number"
        DataGridView1.Columns(1).DataPropertyName = "MyItemNumber"
        DataGridView1.Columns(1).Width = 150

        DataGridView1.Columns(2).Name = "Description"
        DataGridView1.Columns(2).HeaderText = "Description"
        DataGridView1.Columns(2).DataPropertyName = "Description"
        DataGridView1.Columns(2).Width = 240

Open in new window


Private Sub ContextMenuStrip1_Click(sender As System.Object, e As System.EventArgs) Handles ContextMenuStrip1.Click
Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = "Data Source=myserver;Initial Catalog=mydb;Integrated Security=True"

        con.Open()

        cmd.Connection = con

cmd.CommandText = "DELETE From IQuote where QuoteNumber = @QuoteNumber"
cmd.Parameters.AddWithValue("@QuoteNumber", DataGridView1.Columns("QuoteNumber"))
cmd.ExecuteNonQuery()

con.Close()

    End Sub

Open in new window

0
Comment
Question by:printmedia
[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
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39882798
Change

cmd.Parameters.AddWithValue("@QuoteNumber", DataGridView1.Columns("QuoteNumber"))

to

cmd.Parameters.AddWithValue("@QuoteNumber", DataGridView1.SelectedRows(0).Cells("QuoteNumber").Value)
0
 

Author Comment

by:printmedia
ID: 39882815
Thank you for your reply.

Now I get the following error: "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index."

At the line of code you suggested.

Could it be because when the end user right clicks it only highlights the cell that is right-clicked and not the entire row? Or could it be I'm not displaying the QuoteNumber as a column in the datagridview control?
0
 

Author Comment

by:printmedia
ID: 39883012
The SelectionMode was the problem. I had it set to Cell Select, I switched back to FullRowSelect and your suggested line of code works and deletes the row:

cmd.Parameters.AddWithValue("@QuoteNumber", DataGridView1.SelectedRows(0).Cells("QuoteNumber").Value)

Open in new window


Thanks again.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39883246
Glad to help :-)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

764 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