2017-09-19 5 views
0

私はC#でWindowsフォームアプリケーション用のdatagridviewオブジェクトを持っています。ユーザーはいくつかの行を追加できますが、これは任意の数に制限されているため、ユーザーはあまりにも多くの行を入力することはできません。すべての行をdatagridviewに収めるには垂直スクロールバーが表示されないC#?

すべての行(行の高さとフォントサイズ)をデータグリッドビューに収まるように自動サイズ設定し、垂直スクロールバーが表示されないようにします。助言がありますか?

おかげで、

答えて

0

本当にありがとうございます、あなたの答えは本当に私が問題を解決するのを助けました!それの一部は私のために働かなかったので、私はそれらを変更しました。フォントサイズと行の高さの間に関係があります。私は3つの異なる行の高さに3つの最良のフォントサイズを見つけ、特定の行の高さに最適なフォントサイズを見つける回帰を行いました。

private void ResizeRows() 
    { 
     // Get the height of the header row of the DataGridView 
     int headerHeight = this.dataGridView1.ColumnHeadersHeight; 

     // Calculate the available space for the other rows 
     int availableHeight = this.dataGridView1.Height - headerHeight; 

     float rowSize = (float)availableHeight/(float)this.dataGridView1.Rows.Count; 

     float fontSize = 0.8367F * rowSize - 3.878F; 

     // Resize each row in the DataGridView 
     foreach (DataGridViewRow row in this.dataGridView1.Rows) 
     { 
      row.Height = (int)rowSize; 
      row.DefaultCellStyle.Font= new Font(dataGridView1.Font.FontFamily, fontSize, GraphicsUnit.Pixel); 
     } 
    } 
0

あなたは、行ごとの使用可能なスペースを計算することができ、それらの全てのサイズを変更:

private void ResizeRows() 
{ 
    // Calculate the font size 
    float fontSize = calculateFontSize(); 

    // Resize the font of the DataGridView 
    this.dataGridView1.Font = new Font(this.dataGridView1.Font.FontFamily, fontSize); 

    // Get the height of the header row of the DataGridView 
    int headerHeight = this.dataGridView1.Columns[0].Height; 

    // Calculate the available space for the other rows 
    int availableHeight = this.dataGridView1.Height - headerHeight - 2; 

    float rowSize = (float)availableHeight/(float)this.dataGridView1.Rows.Count; 

    // Resize each row in the DataGridView 
    foreach (DataGridViewRow row in this.dataGridView1.Rows) 
    { 
     row.Height = (int)rowSize; 
    } 
} 

あなたのDataGridViewのイベントの2に、このメソッドの呼び出しを追加することができます

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) 
    { 
     this.ResizeRows(); 
    } 

    private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) 
    { 
     this.ResizeRows(); 
    } 

このように、DataGridViewは、行が追加または削除されるたびにその行のサイズを変更する必要があります。方法で異なるフォントサイズで試すことができますcalculateFontSize

関連する問題