0
我有一個DataGridView中選擇一個行包含列複選框,如何檢索datagridview的C#
現在我想,當我從複選框選擇的DataGridView一行,我點擊按鈕保存我找回行以及一些價值 如何請和謝謝你提前如下
我的WinForms:
守則如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace gestion_attachements_decomptes
{
public partial class ajouter_attachement : Form
{
public ajouter_attachement(string num_marche,string libelle_fournisseur)
{
InitializeComponent();
textBox1.Text = num_marche;
textBox2.Text = libelle_fournisseur;
}
private void ajouter_attachement_Load(object sender, EventArgs e)
{
//désactiver button enregistrer
button1.Enabled = false;
//ajouter checkbox dans datagrid view
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
chk.HeaderText = "";
chk.Name = "CheckBox";
dataGridView2.Columns.Add(chk);
dataGridView2.AllowUserToAddRows = false;
dataGridView2.AllowUserToDeleteRows = false;
}
private void button4_Click(object sender, EventArgs e)
{
Program.cmd.CommandText = "select * from bon_reception_marche where Date_reception between '" + dateTimePicker1.Value.Date + "' and '" + dateTimePicker2.Value.Date + "'";
Program.dr = Program.cmd.ExecuteReader();
while (Program.dr.Read())
{
dataGridView2.Rows.Add(Program.dr[2], Program.dr[3], Program.dr[5], Program.dr[6], Program.dr[7], Program.dr[8], Program.dr[9], Program.dr[10], Program.dr[11], Program.dr[12]);
}
Program.dr.Close();
}
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 10/*myColumn*/ && e.RowIndex >= 0 /*myRow*/)
{
button1.Enabled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
// index of the checkbox column
int colIndex = dataGridView2.Columns["CheckBox"].Index;
var rows = dataGridView2.Rows
.Cast<DataGridViewRow>()
.Where(row => row.Cells[colIndex].Value != null)
.Where(row => (bool)row.Cells[colIndex].Value)
.ToList();
// loop through entire DataGridView and see if checkbox is checked
foreach (DataGridViewRow row in dataGridView2.Rows)
{
// checked if the cell's value is true
if ((bool)rows.Cells[colIndex].Value)
{
MessageBox.Show("ok");
}
}
////if (dataGridView2.Columns == 10)
////{
//// double montant = Convert.ToDouble(dataGridView2.CurrentRow.Cells["Montant"].Value);
////}
//int id_br = Convert.ToInt32( dataGridView2.CurrentRow.Cells["Id_bon_reception_marche"].Value);
//Program.cmd.CommandText = "";
//Program.cmd.ExecuteNonQuery();
//MessageBox.Show("c'est ajouté avec succés");
//Program.cmd.Parameters.Clear();
}
}
}
聯播按鈕點擊事件讓所有從網格中的行,然後遍歷行,看是否複選框被選中。 – TheUknown
你可以給我一個例子 –