Hi Appache,
Using this article i have created the sample.
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
public partial class Form1 : Form
{
private const string ConnectionString = @"Data Source=.;Initial Catalog=Test;UID=sa;PWD=pass@123;";
public Form1()
{
InitializeComponent();
//Set Columns Count
dataGridView1.ColumnCount = 3;
//Hide the last blank line
dataGridView1.AllowUserToAddRows = false;
//Add Columns
dataGridView1.Columns[0].Name = "CustomerId";
dataGridView1.Columns[0].HeaderText = "CustomerId Id";
dataGridView1.Columns[0].DataPropertyName = "CustomerId";
dataGridView1.Columns[0].Width = 100;
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].DataPropertyName = "Name";
dataGridView1.Columns[1].Width = 100;
dataGridView1.Columns[2].Name = "Country";
dataGridView1.Columns[2].HeaderText = "Country";
dataGridView1.Columns[2].DataPropertyName = "Country";
dataGridView1.Columns[2].Width = 100;
dataGridView1.MultiSelect = true;
this.BindGrid();
}
private void BindGrid()
{
dataGridView1.DataSource = null;
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to delete this data?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
List<int> id = ids.Distinct().ToList();
for (int i = 0; i < id.Count; i++)
{
using (SqlConnection con = new SqlConnection(ConnectionString))
{
string query = "DELETE FROM Customers WHERE CustomerId = @Id";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", id[i]);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
this.BindGrid();
}
}
protected static List<int> ids = new List<int>();
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
ids.Add(Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value));
dataGridView1.Rows[e.RowIndex].Selected = true;
}
}
}
VB.Net
Public Class Form1
Private Const ConnectionString As String = "Data Source=.;Initial Catalog=Test;UID=sa;PWD=pass@123;"
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set Columns Count
dataGridView1.ColumnCount = 3
'Hide the last blank line
dataGridView1.AllowUserToAddRows = False
'Add Columns
dataGridView1.Columns(0).Name = "CustomerId"
dataGridView1.Columns(0).HeaderText = "CustomerId Id"
dataGridView1.Columns(0).DataPropertyName = "CustomerId"
dataGridView1.Columns(0).Width = 100
dataGridView1.Columns(1).HeaderText = "Name"
dataGridView1.Columns(1).Name = "Name"
dataGridView1.Columns(1).DataPropertyName = "Name"
dataGridView1.Columns(1).Width = 100
dataGridView1.Columns(2).Name = "Country"
dataGridView1.Columns(2).HeaderText = "Country"
dataGridView1.Columns(2).DataPropertyName = "Country"
dataGridView1.Columns(2).Width = 100
dataGridView1.MultiSelect = True
Me.BindGrid()
End Sub
Private Sub BindGrid()
dataGridView1.DataSource = Nothing
Using con As New SqlConnection(ConnectionString)
Using cmd As New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
dataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
If MessageBox.Show("Are you sure you want to delete this data?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
Dim id As List(Of Integer) = ids.Distinct().ToList()
For i As Integer = 0 To id.Count - 1
Using con As SqlConnection = New SqlConnection(ConnectionString)
Dim query As String = "DELETE FROM Customers WHERE CustomerId = @Id"
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Id", id(i))
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
Me.BindGrid()
End If
End Sub
Protected Shared ids As List(Of Integer) = New List(Of Integer)()
Private Sub dataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataGridView1.CellClick
If e.RowIndex >= 0 Then
ids.Add(Convert.ToInt32(dataGridView1.CurrentRow.Cells(0).Value))
dataGridView1.Rows(e.RowIndex).Selected = True
End If
End Sub
End Class