【正文】
eritedStyle property inherits its values from the following properties.1. 2. AlternatingRowsDefaultCellStyle (only for cells in rows with odd index numbers)3. RowsDefaultCellStyle4. DefaultCellStyleFor each property in a DataGridViewCellStyle object returned by an InheritedStyle property, the property value is obtained from the first cell style in the appropriate list that has the corresponding property set to a value other than the DataGridViewCellStyle class defaults.The following table illustrates how the ForeColor property value for an example cell is inherited from its containing column. Property of type DataGridViewCellStyle Example ForeColor value for retrieved object AlternatingRowsDefaultCellStyl。You can define appearance and formatting styles for individual cells, for cells in specific columns and rows, or for all cells in the control by setting the properties of the DataGridViewCellStyle objects accessed through various DataGridView control properties. Additionally, you can modify these styles dynamically based on factors such as the cell value by handling the CellFormatting event.Each cell within the DataGridView control can have its own style, such as text format, background color, foreground color, and font. Typically, however, multiple cells will share particular style characteristics. Groups of cells that share styles may include all cells within particular rows or columns, all cells that contain particular values, or all cells in the control. Because these groups overlap, each cell may get its styling information from more than one place. For example, you may want every cell in a DataGridView control to use the same font, but only cells in currency columns to use currency format, and only currency cells with negative numbers to use a red foreground color. The DataGridViewCellStyle ClassThe DataGridViewCellStyle class contains the following properties related to visual style: BackColor and ForeColor, SelectionBackColor and SelectionForeColor, FontThis class also contains the following properties related to formatting: Format and FormatProvider, NullValue and DataSourceNullValue, WrapMode, Alignment, Padding Using DataGridViewCellStyle ObjectsYou can retrieve DataGridViewCellStyle objects from various properties of the DataGridView, DataGridViewColumn, DataGridViewRow, and DataGridViewCell classes and their derived classes. If one of these properties has not yet been set, retrieving its value will create a new DataGridViewCellStyle object. You can also instantiate your own DataGridViewCellStyle objects and assign them to these properties. You can avoid unnecessary duplication of style information by sharing DataGridViewCellStyle objects among multiple DataGridView elements. Because the styles set at the control, column, and row levels filter down through each level to the cell level, you can also avoid style duplication by setting only those style properties at each level that differ from the levels above. This is described in more detail in the Style Inheritance section that follows.The following table lists the primary properties that get or set DataGridViewCellStyle objects. Property Classes Description DefaultCellStyleDataGridView, DataGridViewColumn, DataGridViewRow, and derived classes Gets or sets default styles used by all cells in the entire control (including header cells), in a column, or in a row.RowsDefaultCellStyleDataGridViewGets or sets default cell styles used by all rows in the control. This does not include header cells.AlternatingRowsDefaultCellStyleDataGridViewGets or sets default cell styles used by alternating rows in the control. Used to create a ledgerlike effect.RowHeadersDefaultCellStyleDataGridViewGets or sets default cell styles used by the control39。如果你在綁定模式下創(chuàng)建了非綁定列,你必須實(shí)現(xiàn)虛擬模式,這樣在根據(jù)綁定列排序時(shí)可以維護(hù)非綁定列的值。 你可以在綁定模式下添加非綁定列,在你希望顯示一個(gè)按鈕列或者鏈接列讓用戶操作一些特定行時(shí)這顯得很有用,另外也可以用非綁定列顯示一些由綁定列計(jì)算而得到的值。該功能準(zhǔn)確的實(shí)現(xiàn)方式取決于數(shù)據(jù)模型的實(shí)現(xiàn)方式及其事務(wù)機(jī)制,例如,提交的時(shí)候是針對(duì)單元格還是行。 你將DataGridView綁定到緩存的數(shù)據(jù),然后用代碼控制數(shù)據(jù)行的存取。但當(dāng)你的數(shù)據(jù)源是BindingSource ,那就不用再額外實(shí)現(xiàn)更改通知了。 BindingListT類也可以在一個(gè)類的基礎(chǔ)上創(chuàng)建自定義列表(list)。只有實(shí)現(xiàn)了IBindingList接口的數(shù)據(jù)源支持更改通知。 任意實(shí)現(xiàn)了IBindingList 接口的類,比如BindingList ;如果使用的數(shù)據(jù)源包含多個(gè)列表(list)或數(shù)據(jù)表(table),你還需要設(shè)置控件的DataMember屬性,該屬性為字符串類型,用于指定要綁定的列表或數(shù)據(jù)表。此時(shí)你可以設(shè)置DataSource屬性,將數(shù)據(jù)源綁定到DataGridView控件。此時(shí)你不是像綁定模式中那樣將DataGridView控件直接指向一個(gè)數(shù)據(jù)源,而是手動(dòng)去生成控件。d with other error contextsInitialValueRestorationWhen exceptions occur while either initializing the editing control/cell39。s formatted value, so the error context is OR39。 初始化編輯控件\單元格的值(通過設(shè)置單元格的FormattedValue屬性或調(diào)用單元格的InitializeEditingControl方法)167。 刷新一個(gè)編輯 (通過調(diào)用RefreshEdit方法)167。 DataError事件將DataError事件獨(dú)立出來作為一個(gè)主題,是因?yàn)樵诓僮鲾?shù)據(jù)時(shí),經(jīng)常會(huì)遭遇DataError事件。 DataSourceNullValue屬性,如果NullValue屬性被命名為FormattedNullValue的話,但最后還是采用了DataSourceNullValue,這樣更直觀準(zhǔn)確。該屬性的默認(rèn)值取決于所在列的類型,見下圖:DataGridView列類型TextBoxColumn (“”)ImageColumn空的圖像()ComboBoxColumn (“”)ButtonColumn (“”)LinkColumn (“”)CheckBoxColumn默認(rèn)值取決于ThreeState屬性的值,如果為true, ,否則為unchecked。使用單元格Style的NullValue屬性和DataSourceNullValue 屬性,你可以改變DataGridView處理null值的方式。參看本文檔后面關(guān)于Virtual Mode的主題。 關(guān)于新行,還要注意:你不能將新行的Visible屬性值設(shè)置為false,否則會(huì)觸發(fā)一個(gè)InvalidOperationException類型的異常。 新行的排序在非綁定模式下,新行總是添加在DataGridView的最后一行,即使已經(jīng)對(duì)數(shù)據(jù)排序。如果你要自定義這個(gè)圖標(biāo),就需要?jiǎng)?chuàng)建一個(gè)自定義的DataGridViewRowHeaderCell 類。對(duì)于DataGridViewImageCell類型的單元格,其初始值為一個(gè)占位圖片,其它類型的則為null。如果用戶此時(shí)按下Escape鍵,“新”的新行會(huì)被移除,這會(huì)觸發(fā)UserDeletingRow事件,它的事件處理函數(shù)的第二個(gè)參數(shù)的屬性Row指定了“新”的新行。如果你嘗試這么做,會(huì)拋出InvalidOperationException 類型的異常。用戶也不能刪除新行。 [CustomerID].Value = NewCustomerId()。 [City].Value = Redmond。 為生成的新行添加默認(rèn)值當(dāng)用戶選擇新行作為當(dāng)前行,DataGridView會(huì)觸發(fā)DefaultValuesNeeded事件。DataGridView控件支持這個(gè)功能,提供了一個(gè)用于添加新記錄的行,而這一行總是顯示為最后一行,并在該行的標(biāo)題單元格標(biāo)以星號(hào)(*)。 顯示錯(cuò)誤信息一旦遭遇了無效的輸入數(shù)據(jù),你通常需要通知用戶。了解所有這些程序的發(fā)生順序會(huì)對(duì)你很有幫助。 數(shù)據(jù)輸入和驗(yàn)證的相關(guān)事件用戶輸入數(shù)據(jù)時(shí)-對(duì)其所在的行或單元格,你可能希望驗(yàn)證這些數(shù)據(jù),在遇到無效數(shù)據(jù)時(shí)通知用戶。你需要驗(yàn)證用戶輸入的數(shù)據(jù),或者需要對(duì)數(shù)據(jù)進(jìn)行格式化。你可以處理CellContentClick事件來相應(yīng)用戶的點(diǎn)擊動(dòng)作。 常見問題1) 如何能夠在ComboBox類型的單元格中輸入數(shù)據(jù)?2) How do I handle the SelectedInde