Nasıl Yapılır

C# İle Örnekler #34

c

C

Datagrigview’ de Çoklu Seçim ve Silme

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace datgridcoklusilme
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection con;
        SqlDataAdapter da;
        SqlCommand cmd;
        DataSet ds;
 
        void doldur()
        {
            con = new SqlConnection("server=.; Initial Catalog=sirket;Integrated Security=SSPI");
            da = new SqlDataAdapter("Select *From musteriler", con);
            ds = new DataSet();
            con.Open();
            da.Fill(ds, "musteriler");
            dataGridView1.DataSource = ds.Tables["musteriler"];
            con.Close();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            doldur();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            List selectedItem = new List();
            DataGridViewRow drow = new DataGridViewRow();
            for (int i = 0; i < = dataGridView1.Rows.Count - 1; i++)
            {
                drow = dataGridView1.Rows[i];
                if (Convert.ToBoolean(drow.Cells[4].Value) == true) //checkbox seçiliyse 
                {
                    string id = drow.Cells[0].Value.ToString();
                    selectedItem.Add(id); //seçiliyse listeye ekle
                }
            }
            con.Open();
            foreach (string s in selectedItem) //çoklu silme işlemi gerçekleşiyor
            {
                SqlCommand cmd = new SqlCommand("delete from musteriler where Musteri_Id='" + s + "'", con);
                int result = cmd.ExecuteNonQuery();
            }
            con.Close();
           // dataGridView1.Rows.Clear();
            doldur();  
        }
    }
}

 

Yazar Hakkında

Hız Hosting

Yorum Yap