我的C#應用程序中有一個DataGridView。使用設計人員,我在DGV本身上設置了AlternatingRowsDefaultCellStyle
以及DefaultCellStyle
屬性。這兩種樣式都有一個填充值0, 0, 5, 0
。我沒有爲DataGridView Tasks加載項中的Edit Columns
菜單中的任何DGV列設置任何自定義樣式。在單個DataGridView列中更改所有單元格的填充
DGV中有一列是圖像列,我爲每一行繪製一個小圖。我想從此列中的所有單元格中刪除填充,因此沒有填充應用於單元格,從而在圖表的最後留下一些空白。
我嘗試過的每個下列事情都不會從列中的任何單元格中移除填充,但也不會拋出任何異常。
// first attempt
// taken from http://social.msdn.microsoft.com/Forums/eu/winforms/thread/a9227253-8bb4-429a-a700-8a3a255afe4d
deviceGrid.Columns["GProduction"].DefaultCellStyle.Padding = new Padding(0);
// second attempt
DataGridViewCellStyle style = deviceGrid.Columns["Graph"].DefaultCellStyle; // also tried Clone()
style.Padding = new Padding(0);
deviceGrid.Columns["GProduction"].DefaultCellStyle = style;
// third attempt
DataGridViewColumn col = deviceGrid.Columns["Graph"];
DataGridViewImageCell icell = new DataGridViewImageCell();
icell.Style.Padding = new Padding(0);
col.CellTemplate = icell;
我猜想,也許從本身是壓倒我試圖爲列設置默認單元格樣式,但如果是這樣的話,有什麼事情,我需要做些什麼來防止這種在DataGridView的DefaultCellStyle
填充?
SOLUTION:
後跟隨由jmh_gr提供我發現這個問題是,對在DataGridView本身DefaultCellStyle
最後被繼承的細胞,所以我不得不從DGV屬性中刪除填充,並將其應用除了我不想填充的列之外的所有列。
花一些時間與[在Windows單元格樣式窗體DataGridView控制]( http://msdn.microsoft.com/en-us/library/1yef90x0.aspx),它詳細介紹了樣式繼承。沒有看到所有的代碼和GUI集樣式,很難確切知道樣式堆棧是什麼。 – 2012-02-14 19:53:20
@jmh_gr感謝您指出了這一點,我現在看到它繼承了DGV DefaultCellStyle LAST的樣式,因爲這是我設置填充的位置,它覆蓋了我在列或單元格本身上設置的0值。我相信解決的辦法是將5px填充應用到需要填充的單元格,並在默認樣式中留下0填充。 – drew010 2012-02-14 20:20:43
而不是將[已解決]放入標題並編輯問題,請將解決方案作爲答案。幾天後,您可以回來並將其標記爲已接受。 – 2012-02-14 23:00:03