Wie fit DataGridView Breite und Höhe, um seinen Inhalt?

Habe ich eine Methode erstellen DataGridView dynamisch, aber wenn es ist oben gezeigt, ist die Breite größer als die content-Breite (Gesamt Breite der Spalten). Auch die Höhe hat nicht genug Länge zu treffen, die Länge der Zeilen.

Ich habe versucht mit dieser Methode, aber es hat nicht funktioniert:

    DataGridView CreateInputBox(int proc, int mac)
        {
            DataGridView databox = new DataGridView();
            for (int i = 0; i < mac; i++)
            {
                databox.Columns.Add("col" + (i + 1), "M" + (i + 1));
                databox.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                databox.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            databox.RowTemplate.DefaultHeaderCellType = typeof(CustomHeaderCell);
            databox.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            databox.RowHeadersDefaultCellStyle.Padding = new Padding(2);
            for (int i = 0; i < proc; i++)
            {
                databox.Rows.Add();
                databox.Rows[i].HeaderCell.Value = "P" + (i + 1);
            }
            databox.DefaultCellStyle.SelectionBackColor = Color.LightGray;
            databox.AllowUserToAddRows = false;
            databox.AllowUserToDeleteRows = false;
            databox.AllowUserToOrderColumns = false;
            databox.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            databox.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

            //This block doesn't work
            var totalHeight = databox.Rows.GetRowsHeight(DataGridViewElementStates.None);
            var totalWidth = databox.Columns.GetColumnsWidth(DataGridViewElementStates.None);
            databox.Width = totalWidth;
            databox.Height = totalHeight;
            //
            return databox;
        }
    public class CustomHeaderCell : DataGridViewRowHeaderCell
    {
        protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
        {
            var size1 = base.GetPreferredSize(graphics, cellStyle, rowIndex, constraintSize);
            var value = string.Format("{0}", this.DataGridView.Rows[rowIndex].HeaderCell.FormattedValue);
            var size2 = TextRenderer.MeasureText(value, cellStyle.Font);
            var padding = cellStyle.Padding;
            return new Size(size2.Width + padding.Left + padding.Right, size1.Height);
        }
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.Background);
            base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
            TextRenderer.DrawText(graphics, string.Format("{0}", formattedValue), cellStyle.Font, cellBounds, cellStyle.ForeColor);
        }
    }

Ergebnis:

Wie fit DataGridView Breite und Höhe, um seinen Inhalt?

Wie Sie sehen können die Breite des DataGridView-Steuerelement ist so lang. Wie kann ich es passend für beide dimension?

  • Mögliche Duplikate von DataGridView Anpassen und Füllen
  • Was ist CustomHeaderCell ?
  • Es ist eine Klasse zu autofit der Zeile header-container, um seinen Inhalt zu text. Ich habe es von stackoverflow.com/questions/37647662/... das ist meine andere Frage. Ich aktualisierte die Frage hinzufügen der CustomHeaderCell.
  • Ich glaube nicht, dass es die gleiche Frage mit dieser. Die Frage, die Sie gab, ist über die Spalte autosize , aber meine Frage zu DataGridView selbst (container).
  • Ah. Ich habe aktualisiert die Abbildung zeigen das Ergebnis bei der CustomHeaderCell Klasse..
  • Ich sehe. Es ist nur ein kleines detail, nicht viel wichtiger sind. Nochmals vielen Dank @TaW.

InformationsquelleAutor Ali Tor | 2016-06-06
Schreibe einen Kommentar