Alex Alex - 5 days ago 9
C# Question

Get value of a selected checkbox row in DatagridView

I have a DatagridView which contains row and data. I've added checkboxs to select one of the row (1) and then generate a PDF with the data of the selected row (2) (see picture) :

Screenshot of the DGV

My code contains a part which check if checkbox is 1 or 0 and then I don't know how to get the data of the "checked row".. See

private void button_generer_pdf_Click(object sender, EventArgs e)
{
List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGrid_factures.Rows)
{
if (Convert.ToBoolean(row.Cells[column_action.Name].Value) == true)
{
MessageBox.Show("OK!"); // Just to check if it undestands I've checked the row
//And then here I want to get highlighted data on the screenshot to create my Pdf
}
}
//PDF Generation here

Answer

The same way you got the data from your selection column "row.Cells[column_action.Name].Value" by changing it to be the right name so maybe

row.Cells["NOM"].Value

or you can use the array number should you know it eg

rows.Cells[3].Value