-
March 10th, 2009, 01:26 PM
#1
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.
-
March 12th, 2009, 03:56 AM
#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.
-
March 12th, 2009, 10:49 PM
#3
Re: Datagridview delete row
Add a checkbox column to the list, and have them check the name(s) and then click DELETE
-
March 13th, 2009, 02:41 AM
#4
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.
-
March 13th, 2009, 09:22 AM
#5
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
-
June 3rd, 2014, 03:56 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|