 
 
DataGridView da Seçili CheckBox taki Satırı Silme
Bu makalede DataGridView nesnesine checkbox sütunu ekleyeceğiz. Ayrıca sql veritabanımıza bağlanıp bilgileri dataGridView nesnesinde göstereceğiz. Son olarak checkbox ı seçili satırı sileceğiz. Aşağıdaki şekilleri inceleyin. Bahadır
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        string str;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Randevu Kayıt Sistemi...Bahadır ŞAHİN";
            this.MaximizeBox = false;
            this.Location = new Point(50, 100);
            //checkbox nesnesi tanımlanıyor
            DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn();
            //Tanımlanan checkbox ın sütun ismi, boyutu belirleniyor
            checkbox.DataPropertyName = "CheckBox";
            checkbox.HeaderText = "Sec";
            checkbox.Width = 80;
            checkbox.Name = "cb1";
            // checkbox sütunu dataGridView a ekleniyor
            dataGridView1.Columns.Add(checkbox);
 
            //dataGridView özellikleri belirleniyor.  
            DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
            columnHeaderStyle.BackColor = Color.Aqua;
            columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
            dataGridView1.RowTemplate.DefaultCellStyle.BackColor = Color.LightYellow;
            dataGridView1.RowTemplate.DefaultCellStyle.ForeColor = Color.Navy;
            dataGridView1.RowTemplate.DefaultCellStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
            dataGridView1.RowTemplate.Height = 20;
            dataGridView1.RowTemplate.MinimumHeight = 20;
System.Data.SqlClient.SqlConnection baglan = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\rezervasyon.mdf;Integrated Security=True;User Instance=True");
            System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter();
            adp.SelectCommand = new System.Data.SqlClient.SqlCommand("Select * From randevu_kayit", baglan);
            System.Data.SqlClient.SqlCommandBuilder CB = new System.Data.SqlClient.SqlCommandBuilder(adp);
            baglan.Open();
            DataSet ds = new DataSet();
            adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
            baglan.Close();
            int i;
            for (i = 1; i <= dataGridView1.Columns.Count - 1; i++)
            {
dataGridView1.Columns[i].Width = 200;
                if (i == dataGridView1.Columns.Count - 2)
                {
                    dataGridView1.Columns[i].Width = 80;
                }
                if (i == dataGridView1.Columns.Count - 1)
                {
                    dataGridView1.Columns[i].Width = 80;
                }
            }
}
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
}
        private void btnSil_Click(object sender, EventArgs e)
        {
            DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn();
            checkbox.Name = "cb1";
int i;
            for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["cb1"].Value) == true)
                {
System.Data.SqlClient.SqlConnection baglan = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\rezervasyon.mdf;Integrated Security=True;User Instance=True");
                    System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter();
                    adp.SelectCommand = new System.Data.SqlClient.SqlCommand("Select * From randevu_kayit", baglan);
                    System.Data.SqlClient.SqlCommandBuilder CB = new System.Data.SqlClient.SqlCommandBuilder(adp);
                    baglan.Open();
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
                    str = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[1]);
dataGridView1.Rows[i].Cells["cb1"].Value = true;
baglan.Close();
                }
            }
            sil();
        }
         void sil()
        {
            int i;
            for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["cb1"].Value) == true)
                {
                    System.Data.SqlClient.SqlConnection baglan = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\rezervasyon.mdf;Integrated Security=True;User Instance=True");
                    string sqlstr;
SqlCommand sqlCmd;
                    //id = CType(txtid.Text, Integer)
                    dataGridView1.Rows[i].Cells["cb1"].Value = true;
                    sqlstr = "DELETE FROM randevu_kayit WHERE Ad = ‘" + str + "‘";
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
                    sqlCmd = new SqlCommand(sqlstr, baglan);
da.DeleteCommand = sqlCmd;
                    try
                    {
baglan.Open();
                        DataSet ds = new DataSet();
                        DataTable dt;
i = sqlCmd.ExecuteNonQuery();
                        if (i > 0)
                        {
MessageBox.Show("Seçili Kayıt Silindi... Bahadirsa");
da.Update(ds, "randevu_kayit");
ds.Tables["randevu_kayit"].Reset();
da.Fill(ds, "randevu_kayit");
dt = ds.Tables["randevu_kayit"];
                        }
                    }
                    catch (Exception ex)
                    {
                    }
// MessageBox.Show("Bahadirsa Hata : " & vbCrLf & ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    finally
                    {
                        baglan.Close();
db_baglan();
}
}
            }
        }
   
     private void db_baglan()
     {
System.Data.SqlClient.SqlConnection baglan = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\rezervasyon.mdf;Integrated Security=True;User Instance=True");
         System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter();
         adp.SelectCommand = new System.Data.SqlClient.SqlCommand("Select * From randevu_kayit", baglan);
         System.Data.SqlClient.SqlCommandBuilder CB = new System.Data.SqlClient.SqlCommandBuilder(adp);
        
         baglan.Open();
         DataSet ds = new DataSet();
         adp.Fill(ds);
        
         dataGridView1.DataSource = ds.Tables[0];
        
         baglan.Close();
     }
}
}
//Bir sonraki makalede görüşmek üzere. Bahadır