Merhaba arkadaşlar bu
makalemizde ilk önce DataGridView nesnesine CheckBox sütun ekliyoruz. Sonra
MySql veritabanımızdaki tablomuzdaki verileri DataGridView da gösteriyoruz. Son
olarak CheckBox da seçili satırları diğer DataGriew da gösterimi sağlıyoruz.
Şekil 1
Form1.cs
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 MySql.Data.MySqlClient;
namespace datagridview_checkbox_row_datagridview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,
EventArgs e)
{
DataGridViewCheckBoxColumn cbox = new DataGridViewCheckBoxColumn();
//add checkbox
column. datagridview nesnesine checkbox sutun ekliyoruz
cbox.DataPropertyName = "CheckBox";
cbox.HeaderText
= "Select";
cbox.Width = 60;
cbox.Name = "cbox";
dataGridView1.Columns.Add(cbox);
MySqlConnection
con = new MySqlConnection("Server=localhost;Database=book;Uid=root;Pwd='1234';AllowUserVariables=True;UseCompression=True;");
con.Open();
MySqlCommand cmd
= new MySqlCommand("Select
* From book.worldclassics", con);
MySqlDataAdapter
da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();
dataGridView2.ColumnCount = 4;
dataGridView2.Columns[0].Name = "Id";
dataGridView2.Columns[1].Name = "Author";
dataGridView2.Columns[2].Name = "Book";
dataGridView2.Columns[3].Name = "Price";
}
private void
dataGridView1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs e)
{
DataGridViewRow
row = this.dataGridView1.Rows[e.RowIndex];
if
(((Convert.ToBoolean(row.Cells[0].Value) == false)))
{
row.DefaultCellStyle.BackColor = Color.White;
}
else
{
row.DefaultCellStyle.BackColor = Color.BlueViolet;
row.DefaultCellStyle.ForeColor = Color.White;
}
}
private void
dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//Check to
ensure that the row CheckBox is clicked.
if (e.RowIndex
>= 0 && e.ColumnIndex == 0)
{
//Loop and
uncheck all other CheckBoxes.
foreach
(DataGridViewRow row in dataGridView1.Rows)
{
if (row.Index ==
e.RowIndex)
{
DataGridViewRow newRow = new DataGridViewRow();
newRow.CreateCells(dataGridView2);
newRow.Cells[0].Value = row.Cells["Id"].Value;
newRow.Cells[1].Value =row.Cells["Author"].Value;
newRow.Cells[2].Value = row.Cells["Book"].Value;
newRow.Cells[3].Value = row.Cells["Price"].Value;
dataGridView2.Rows.Add(newRow);
}
else
{
//row.Cells["cbox"].Value
= false;
}
}
}
}
}
}
Bir
makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır
ŞAHİN
0 comments:
Yorum Gönder