1
我想在C#Winform中刷新DataGridView的內容並保持其外觀。刷新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尺寸。
我將不勝感激任何建議,以優化當前的代碼。
後似乎DataColumn類沒有Width屬性,也不包含任何用戶界面相關的屬性。相反,它需要通過dataGridView1.Columns [index]直接訪問它。 – 2013-05-07 17:00:27
嘗試更新 – 2013-05-07 17:02:24
嘗試將var替換爲可訪問Width屬性的DataGridViewColumn。很棒。 – 2013-05-07 17:10:51