Cela peut être fait, vous devrez ajuster le ClientSize lorsqu'une ligne est ajoutée ou supprimée. Cependant, il ne masque pas complètement l'arrière-plan une fois que la barre de défilement verticale apparaît et que la hauteur de la grille n'est pas divisible par la hauteur de la ligne. Ajoutez une nouvelle classe à votre projet et collez le code ci-dessous. Compiler. Déposez le nouveau contrôle du haut de la boîte à outils sur votre formulaire.
using System;
using System.Drawing;
using System.Windows.Forms;
class AutoSizeGrid : DataGridView {
private int gridHeight;
private bool resizing;
protected override void OnClientSizeChanged(EventArgs e) {
if (!resizing) gridHeight = this.ClientSize.Height;
base.OnClientSizeChanged(e);
}
protected override void OnRowsAdded(DataGridViewRowsAddedEventArgs e) {
setGridHeight();
base.OnRowsAdded(e);
}
protected override void OnRowsRemoved(DataGridViewRowsRemovedEventArgs e) {
setGridHeight();
base.OnRowsRemoved(e);
}
protected override void OnHandleCreated(EventArgs e) {
this.BeginInvoke(new MethodInvoker(setGridHeight));
base.OnHandleCreated(e);
}
private void setGridHeight() {
if (this.DesignMode || this.RowCount > 99) return;
int height = this.ColumnHeadersHeight + 2;
if (this.HorizontalScrollBar.Visible) height += SystemInformation.HorizontalScrollBarHeight;
for (int row = 0; row < this.RowCount; ++row) {
height = Math.Min(gridHeight, height + this.Rows[row].Height);
if (height >= gridHeight) break;
}
resizing = true;
this.ClientSize = new Size(this.ClientSize.Width, height);
resizing = false;
if (height < gridHeight && this.RowCount > 0) this.FirstDisplayedScrollingRowIndex = 0;
}
}
+1 pour le surbrillance retardée :) – TheBlastOne
C'est le point culminant "cognitivement désactivé". –