Is it possible to only print the columns in a row in a DataGridView
that have values and exclude the non empty ones? I'm trying to print the ones and only the ones that have actual values stored in it, but right now it is printing all of them.
Here is a screenshot of the actual print document (saved as a pdf): http://imgur.com/HiF9heq
I would like to eliminate the rest of the columns that are empty.
Here is the code I have in place to print and the code that fills the data table:
private void Printdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
qbcDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
// set the left margin of the document to be printed
int leftMargin = e.MarginBounds.Left;
// set the top margin of the document to be printed
int topMargin = e.MarginBounds.Top;
// variable to determine if more pages are to be printed
bool printMore = false;
// temp width
int tmpWidth = 0;
// for the first page to print, set the cell width and header height
if (firstPage)
{
foreach (DataGridViewColumn gridCol in qbcDataGridView.Columns)
{
tmpWidth = (int)(Math.Floor((double)gridCol.Width /
totalWidth * totalWidth *
((double)e.MarginBounds.Width / totalWidth)));
headerHeight = (int)(e.Graphics.MeasureString(gridCol.HeaderText, gridCol.InheritedStyle.Font, tmpWidth).Height) + 2;
// save the width and height of the headers
arrayLeftColumns.Add(leftMargin);
arrayColWidths.Add(tmpWidth);
leftMargin += tmpWidth;
}
}
// loop until all of the grid rows get printed
while (row <= qbcDataGridView.Rows.Count - 1)
{
DataGridViewRow gridRow = qbcDataGridView.Rows[row];
// set the cell height
cellHeight = gridRow.Height + 5;
int count = 0;
// check to see if the current page settings allow more rows to print
if (topMargin + cellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
{
newPage = true;
firstPage = false;
printMore = true;
break;
}
else
{
if (newPage)
{
// draw the header
e.Graphics.DrawString("QBC Directory",
new Font(qbcDataGridView.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left,
e.MarginBounds.Top - e.Graphics.MeasureString("QBC Directory",
new Font(qbcDataGridView.Font, FontStyle.Bold),
e.MarginBounds.Width).Height - 13);
// set the data (now) and the current time
String date = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString();
// draw the date on the print document
e.Graphics.DrawString(date,
new Font(qbcDataGridView.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(date,
new Font(qbcDataGridView.Font, FontStyle.Bold),
e.MarginBounds.Width).Width),
e.MarginBounds.Top - e.Graphics.MeasureString("QBC Directory", new Font(new Font(qbcDataGridView.Font, FontStyle.Bold),
FontStyle.Bold), e.MarginBounds.Width).Height - 13);
// draw the column headers
topMargin = e.MarginBounds.Top;
foreach (DataGridViewColumn gridCol in qbcDataGridView.Columns)
{
if (!string.IsNullOrEmpty(gridCol.HeaderText))
{
// header color
e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
new Rectangle((int)arrayLeftColumns[count], topMargin,
(int)arrayColWidths[count], headerHeight));
// header text box
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int)arrayLeftColumns[count], topMargin,
(int)arrayColWidths[count], headerHeight));
// header string
e.Graphics.DrawString(gridCol.HeaderText,
gridCol.InheritedStyle.Font, new SolidBrush(gridCol.InheritedStyle.ForeColor),
new RectangleF((int)arrayLeftColumns[count], topMargin, (int)arrayColWidths[count], headerHeight), string_format);
}
else
{
break;
}
count++;
}
newPage = false;
topMargin += headerHeight;
}
count = 0;
// draw the column's contents
foreach (DataGridViewCell gridCell in gridRow.Cells)
{
if (gridCell.Value != null)
{
if (!string.IsNullOrEmpty(gridCell.Value.ToString()))
{
e.Graphics.DrawString(gridCell.Value.ToString(),
gridCell.InheritedStyle.Font, new SolidBrush(gridCell.InheritedStyle.ForeColor),
new RectangleF((int)arrayLeftColumns[count], topMargin, (int)arrayColWidths[count], cellHeight), string_format);
}
else
{
break;
}
}
else
{
break;
}
// draw the borders for the cells
e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)arrayLeftColumns[count], topMargin, (int)arrayColWidths[count], cellHeight));
count++;
}
}
row++;
topMargin += cellHeight;
// if more lines exist, print another page
if (printMore)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
and the menu strip item that fills the DataGridView
:
private void MenuViewMembers_Click(object sender, EventArgs e)
{
qbcDataGridView.Font = new Font(qbcDataGridView.Font.FontFamily, 10);
qbcDataGridView.Location = new Point(30, 100);
qbcDataGridView.Size = new Size(1500, 500);
dbConn.Open();
DataTable dt = new DataTable();
DbAdapter = new OleDbDataAdapter("select ID, household_head, birthday, phone, email, address, status, spouse, spouse_birthday, spouse_email, anniversary, spouse_status," +
"child1, child1_birthday, child1_email, child2, child2_birthday, child3_birthday, child4, child4_birthday, child4_email, child5, child5_birthday, child5_email," +
"child6, child6_birthday, child6_email, child7, child7_birthday, child7_email from members", dbConn);
DbAdapter.Fill(dt);
qbcDataGridView.DataSource = dt;
qbcDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
qbcDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
qbcDataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dbConn.Close();
Controls.Add(qbcDataGridView);
}
I think that if only the non-empty values were printed it would format the printed document correctly (see screenshot).
Any help would be greatly appreciated.
Thanks!
Update - I got it so where the empty cells are not shown (http://imgur.com/R0ueyft) but I guess my other question is how to not have the column headers shown as well if the cells are empty. I updated my code to reflect the changes I made.
After filling a DataTable with the data, go through the columns and delete the empty.
DbAdapter.Fill(dt);
for (int i = dt.Columns.Count - 1; i >= 0; i--)
{
if (dt.AsEnumerable().All(row => row[i].ToString() == ""))
{
dt.Columns.RemoveAt(i);
}
}
qbcDataGridView.DataSource = dt;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With