如何打印动态创建最后一行的DataGridView?

时间:2016-12-23 09:39:31

标签: c# asp.net

我动态创建了DataGridView中的最后一个单元格/行,以显示总成员数和支付的总金额,但是当我打印DataGridView时,不打印最后一个单元格/行。

我试图解决这个问题,但是没有得到正确的结果。我怎样才能打印最后一个单元格/行?

这是我在DataGridView中显示数据的代码:

private void button1_Click(object sender, EventArgs e)
{
    SqlCommand cmdshow = new SqlCommand("select Regno,Fullname,Contactno,Address,PaidAmt from NewMember", con);
    SqlDataReader drshow = cmdshow.ExecuteReader();
    dt = new DataTable();
    dt.Load(drshow);
    dataGridViewreport.DataSource = dt;

    dataGridViewreport[3, dataGridViewreport.Rows.Count - 1].Value = "Total Sum";
    dataGridViewreport[0, dataGridViewreport.Rows.Count - 1].Value = "Total Members";

    int r;
    for (r = 0; r < dataGridViewreport.Rows.Count - 1; r++)
    {
        DataGridViewRow row = dataGridViewreport.Rows[r];
    }

    double Total = 0;
    for (int i = 0; i < dataGridViewreport.Rows.Count - 1; i++)
    {
        Total += Convert.ToDouble(dataGridViewreport.Rows[i].Cells["PaidAmt"].Value);

    }
    dataGridViewreport.Rows[dataGridViewreport.Rows.Count - 1].Cells["PaidAmt"].Value = Total;
    dataGridViewreport.Rows[dataGridViewreport.Rows.Count - 1].Cells["Fullname"].Value = r;
 }

此代码用于打印DataGridView:

private void button2_Click(object sender, EventArgs e)
{
    DGVPrinter printer = new DGVPrinter();
    printer.Title = "Member Report";
    printer.PageNumberInHeader = false;
    printer.PorportionalColumns = true;

    printer.HeaderCellAlignment = StringAlignment.Near;
    printer.PrintDataGridView(dataGridViewreport);
  }

0 个答案:

没有答案