如何打印的DataGridView单排 [英] How to print a single row of DataGridView

查看:98
本文介绍了如何打印的DataGridView单排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我一直在寻找这种帮助周并没有得到尚未答案
在这里我去...我有一个DataGridView,这DGV有一个名为ColumnCheckBox(打印)和其他3列是(数目,描述和价格)
当我通过在ColumnCheckBox(打印),单击我想从点击打印按钮提及。而这3列获取该行的价值选择一行将打印他们中的每一个,只有选择行!所有的家伙我的搜索运行以创建从阵列中的和打印后,但我不知道怎么办!

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 !

每一个答案,将尝试与AP preciated

every answer will be tried and appreciated

推荐答案

此方式,您可以使用一些标准,发现一排,例如,你可以找到你首先检查行:

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());

MessageBox.Show(builder.ToString());

甚至你可以把的PrintDocument 表单上,处理的PrintPage 事件来把它们打印到打印机。你还应该把按钮的形式和按钮的点击事件,调用 PrintDocument1.Print();

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));
}

这篇关于如何打印的DataGridView单排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆