Reputation: 83
Hi guys I've been looking for this help for weeks and didn't get yet the answer here I go...I have a datagridview , this DGV has a ColumnCheckBox named ("print") and other 3 Columns being (Number,Description,Price) When I Select a Row by clicking in the ColumnCheckBox("Print") I want to get the value of the row from this 3 columns mentioned .and by clicking on print button it will print each one of them only the selected row! guys all my searches runs to create an array and after print from the array but i don't know how !
every answer will be tried and appreciated
Upvotes: 0
Views: 2574
Reputation: 125197
This way you can find a row using some criteria, for example you can find your first checked row:
var firstCheckedRow = this.myDataGridView.Rows.Cast<DataGridViewRow>()
.Where(row => (bool?)row.Cells["MyCheckBoxColumn"].Value == true)
.FirstOrDefault();
This way you can get value of all cells of a row, for example you can put theme together in a string in different lines:
var builder = new StringBuilder();
firstCheckedRow.Cells.Cast<DataGridViewCell>()
.ToList().ForEach(cell =>
{
builder.AppendLine(string.Format("{0}", cell.Value));
});
Then you can for example show them:
MessageBox.Show(builder.ToString());
Or even you can put a PrintDocument
on your form and handle PrintPage
event to print them to printer. You also should put a Button
on form and in click event of button, call PrintDocument1.Print();
Code:
private void Button1_Click(object sender, EventArgs e)
{
PrintDocument1.Print();
}
PrintDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
var firstCheckedRow = this.myDataGridView.Rows.Cast<DataGridViewRow>()
.Where(row => (bool?)row.Cells["MyCheckBoxColumn"].Value == true)
.FirstOrDefault();
var builder = new StringBuilder();
firstCheckedRow.Cells.Cast<DataGridViewCell>()
.ToList().ForEach(cell =>
{
builder.AppendLine(string.Format("{0}", cell.Value));
});
e.Graphics.DrawString(builder.ToString(),
this.myDataGridView.Font,
new SolidBrush(this.myDataGridView.ForeColor),
new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
}
Upvotes: 1