Datagridview delete row
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Datagridview delete row

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    2

    Datagridview delete row

    Hi,, i am new here. i want solutions for my .net simple program.this is my first program in vb.net and its quite simple one.

    program outline:
    I have a sql databse Records which has 3 columns accountid, passwords and validity. I successfully programmed the insert record function via Add button. I also have Datagridview. I want to have a function which will delete the record from the datagridview & database when i would click the right button. I have properly applied the contextmenu for the datagridview. but However i am couldn't able to function it properly. how to do this. how to delete record from datagridview by using right click function. this is my program. so far i have done this. could anyone resolve this. thank you.

    Code:
    Imports System.DateTime
    Imports system.Data.SqlClient
    Imports System.data.DataTable
    Public Class frmstorage
    
    
    
        Private Sub frmstorage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'Storage_managerDataSet.Records' table. You can move, or remove it, as needed.
            Me.RecordsTableAdapter.Fill(Me.Storage_managerDataSet.Records)
    
        End Sub
    
        Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
            End
        End Sub
    
        Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
            Dim sqlconn As New SqlClient.SqlConnection("Data Source=SUPREME;Initial Catalog=storage_manager;Integrated Security=True")
            sqlconn.Open()
            Dim sqlcom As New SqlClient.SqlCommand()
            txtaccountid.Focus()
    
    
            sqlcom.Parameters.Add(New SqlClient.SqlParameter("@accountid", SqlDbType.VarChar))
            sqlcom.Parameters.Add(New SqlClient.SqlParameter("@passwords", SqlDbType.VarChar))
            sqlcom.Parameters.Add(New SqlClient.SqlParameter("@validity", SqlDbType.DateTime))
    
            Try
                Dim cmd As String
                sqlcom.Parameters("@accountid").Value = txtaccountid.Text
                sqlcom.Parameters("@passwords").Value = txtpassword.Text
                sqlcom.Parameters("@validity").Value = dtpvalidity.Value
    
                cmd = "insert into Records values(@accountid,@passwords,@validity)"
                sqlcom.CommandText = cmd
                sqlcom.Connection = sqlconn
                txtaccountid.Text = ""
                txtpassword.Text = ""
                dtpvalidity.Text = ""
    
                Dim n1 As Integer
                n1 = sqlcom.ExecuteNonQuery
                If n1 > 0 Then
                    DataGridView1.Refresh()
                    MsgBox(n1 & "Record Added Successfully", MsgBoxStyle.Information, "info")
    
                Else
                    MsgBox("Record was not Inserted", MsgBoxStyle.Information, "info")
    
    
                End If
    
            Catch ex As System.Exception
                MsgBox(ex.Message)
            End Try
            sqlconn.Close()
    
            DataGridView1.RefreshEdit()
        End Sub
    
        
    
        Private Sub RemoveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveToolStripMenuItem.Click
            Dim sqlconn As New SqlClient.SqlConnection("Data Source=SUPREME;Initial Catalog=storage_manager;Integrated Security=True")
            sqlconn.Open()
            Dim sqlcom As New SqlClient.SqlCommand
            DataGridView1.Focus()
            sqlcom.Parameters.Add(New SqlClient.SqlParameter("@accountid", SqlDbType.VarChar))
            Try
    
                Dim cmd As String
                Dim i As Integer
                Dim j As String
                j = DataGridView1.SelectedRows(i).ToString
                sqlcom.Parameters("@accountid").Value = j
                cmd = "delete from Records where accountid=@j"
                sqlcom.CommandText = cmd
                sqlcom.Connection = sqlconn
                Dim n1 As Integer
                n1 = sqlcom.ExecuteNonQuery
                If n1 > 0 Then
                    DataGridView1.Visible = True
                Else
                    MsgBox("Invalid")
                End If
    
    
            Catch ex As System.Exception
                MsgBox(ex.Message)
            End Try
            sqlconn.Close()
    
        End Sub
    
        Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            For Each row As DataGridViewRow In DataGridView1.SelectedRows
                DataGridView1.Rows.Remove(row)
            Next
        End Sub
    End Class
    Last edited by PeejAvery; March 10th, 2009 at 05:21 PM. Reason: Added code tags.

  2. #2
    Join Date
    Mar 2009
    Posts
    2

    Re: Datagridview delete row

    no one got answer?.. its very simple for professinal ..looks like no one so genius to solve this one.
    Last edited by Working_Class_Hero; March 12th, 2009 at 04:45 AM.

  3. #3
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,987

    Re: Datagridview delete row

    Add a checkbox column to the list, and have them check the name(s) and then click DELETE
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,845

    Re: Datagridview delete row

    For the right click you should be able to use a popup menu.
    To delete the data you can either issue a delete command to the database then refresh the data grid or issue a delete command to the database and remove the rows fromt he grid via code. The latter would be faster whereas the former would confirm that the delete issued to the db actually removed the records.

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,987

    Re: Datagridview delete row

    It's a context menu, and we need a VB.Net sample (other than the 101 Samples for VB.Net or C#)

    http://www.c-sharpcorner.com/UploadF...rgridview.aspx
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jun 2014
    Posts
    1

    Re: Datagridview delete row

    A simple c# code that delete row data from datagridview by right click..

    private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
    {
    if (e.Button == MouseButtons.Right)
    {
    this.dataGridView1.Rows[e.RowIndex].Selected = true;
    this.rowIndex = e.RowIndex;
    this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];
    this.contextMenuStrip1.Show(this.dataGridView1, e.Location);
    contextMenuStrip1.Show(Cursor.Position);
    }
    }

    private void contextMenuStrip1_Click(object sender, EventArgs e)
    {
    if (!this.dataGridView1.Rows[this.rowIndex].IsNewRow)
    {
    this.dataGridView1.Rows.RemoveAt(this.rowIndex);
    }
    }

    Full C# Code...Delete from datagridview by right ****

    watson

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center