DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; DataGridView1.ColumnHeadersHeight = 50; DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader; // Here we attach an event handler to the cell painting event DataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(DataGridView1_CellPainting);
foreach (DataGridViewColumn GridCol in DataGridView1.Columns) { e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle((int)arrColumnLefts[iCount], iTopMargin, (int)arrColumnWidths[iCount], iHeaderHeight)); e.Graphics.DrawRectangle(Pens.Black, new Rectangle((int)arrColumnLefts[iCount], iTopMargin, (int)arrColumnWidths[iCount], iHeaderHeight)); e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font, new SolidBrush(GridCol.InheritedStyle.ForeColor), new RectangleF((int)arrColumnLefts[iCount], iTopMargin, (int)arrColumnWidths[iCount], iHeaderHeight), strFormat1); iCount++; }
private void printDocument1_BeginPrint(object sender, PrintEventArgs e) { try { iRow = 0; strFormat1 = new StringFormat(); strFormat1.Alignment = StringAlignment.Near; strFormat1.LineAlignment = StringAlignment.Center; strFormat1.Trimming = StringTrimming.EllipsisCharacter; strFormat1.FormatFlags = StringFormatFlags.DirectionVertical; arrColumnLefts.Clear(); arrColumnWidths.Clear(); iCellHeight = 0; //iCount = 0; bFirstPage = true; bNewPage = true; //Calculating Total Widths iTotalWidth = 0; foreach (DataGridViewColumn dgvGridCol in DataGridView1.Columns) { iTotalWidth += dgvGridCol.Width; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }