简体   繁体   中英

stretch rows to fill entire height in DataGridView c# winform

I'm learning a bit on DataGridView

I have a DataGridView that I fill with some columns and row from DataSource

later I want output a bitmap , for it i want that my columns and rows fill entire available space

with columns, I just use (it works fine)

foreach (DataGridViewColumn col in DG_dataGridView.Columns)
{
    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}

My problem that I didn't able to do the same with rows

this my code

//columns stretching
foreach (DataGridViewColumn col in DG_dataGridView.Columns)
{
    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}

var printSet = printDocument1.DefaultPageSettings;
DG_dataGridView.Height = printSet.Bounds.Height;

int headerHeight = DG_dataGridView.ColumnHeadersHeight;
int height = DG_dataGridView.Size.Height - headerHeight;
int rowHeight = height / DG_dataGridView.RowCount; // -1;

//rows stretching
for (int i = 0; i < DG_dataGridView.Rows.Count; i++)
{
    DG_dataGridView.Rows[i].Height = rowHeight;
}

Bitmap bm = new Bitmap(DG_dataGridView.Width, DG_dataGridView.Height);

DG_dataGridView.DrawToBitmap(bm, new Rectangle(0, 0, DG_dataGridView.Width, DG_dataGridView.Height));

bm.Save(System.IO.Directory.GetCurrentDirectory() + @"\latestBitTable.bmp");

I believe this might be caused by the AutoSizeRowsMode property. When it is set to AllCells for example, the rows will be automatically re-sized to fit the cell contents (regardless of how you defined or set it in code).

Perhaps setting it to None might solve your problem.

dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;

The following documentation might be of use to you when programming your wanted behavior: Sizing Options in the Windows Forms DataGridView Control

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM