2010-07-13 36 views

回答

13

Theres是datagridview的columns部分中的一個屬性,用於凍結列。

轉到您的datagridview - >列 - >(列你想凍結) - >冷凍=真

編輯:看來

測試後那麼 只會凍結 左邊的列,datagrid不是正確的。

編輯2:

爲了得到它凍結在 列右側啓用DataGrid中的 「從右至左」 財產。它反轉 列的繪製順序, 允許最右邊的列爲 凍結。

dataGridView1.Columns["columnname"].Frozen = true; 
dataGridView1.RightToLeft = Enabled; 
+0

+1偉大的工作!我沒有想到'RightToLeft'屬性。 – 2010-07-13 18:41:10

1

您可以使用DataGridViewElementStates枚舉值之一。

要麼使用一個索引:

dataGridView1.Columns[0].Frozen = true; 

或使用列名:

dataGridView1.Columns["columnName"].Frozen = true; 

您也可以使用DataGridViewColumnCollection.GetFirstColumn()方法:

dataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Frozen); 

我會personnaly與去索引,因爲您希望兩個第一列凍結。然後,當你想改變這些凍結的列時,你只需要在設計中改變它們的索引。

至於使右側的兩列被凍結,我會簡單地將它們帶到右側,這是一種更符合人體工程學的方式,因爲大部分時間我們都是從左到右閱讀。

0
private void Button4_Click(object sender, System.EventArgs e) 
{ 

    FreezeBand(dataGridView.Rows[0]); 
} 

private void Button5_Click(object sender, System.EventArgs e) 
{ 

    FreezeBand(dataGridView.Columns[1]); 
} 

private static void FreezeBand(DataGridViewBand band) 
{ 
    band.Frozen = true; 
    DataGridViewCellStyle style = new DataGridViewCellStyle(); 
    style.BackColor = Color.WhiteSmoke; 
    band.DefaultCellStyle = style; 
} 
0
dataGridView1.Columns["columnname"].Frozen = true; 
+0

儘管此代碼片段可能會解決問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – DimaSan 2016-12-16 11:42:45