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

Threaded 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 06:21 PM. Reason: Added code tags.

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