2

C# Winform で DataGridView のコンテンツを更新し、その外観を維持したいと考えています。

リフレッシュできる唯一の方法は、グリッドを再初期化する次の関数を使用することです。

データ ソースを更新し、すべての列をクリアして、適切なヘッダー テキストで必要な列を戻します。

private void InitDataGrid()
{
    allItem = DataRepository.LotProvider.GetByIdProduit(detail.IdProduit)
    dataGridView1.DataSource = allItem;
    dataGridView1.Columns.Clear();
    // Get a dictionary of the required column ID / shown text
    Dictionary<string, string> dictionary = InitDisplayedFields();        
    foreach (KeyValuePair<string, string> column in dictionary)
        // If the grid does not contain the key
        if (!dataGridView1.Columns.Contains(column.Key))
        {
            // Add the column (key-value)
            int id = dataGridView1.Columns.Add(column.Key, column.Value);
            // Bind the property
            dataGridView1.Columns[id].DataPropertyName = column.Key;
        }
}

問題は、ユーザーが列を Y サイズから X サイズに拡大し、DataGridView を更新すると、列が Y サイズに戻ることです。

そうしないと、初期化時にグリッドにすべてのデータソース列が表示されるため、Clear() を回避できませんでした。

私が望むのは、すべてのストレッチされた列の X サイズを維持することです。

現在のコードを最適化するための提案をいただければ幸いです。

4

1 に答える 1