1. DataGridView控件概述
    1. DataGridView 控件代码目录(Windows 窗体)
      1. 未绑定数据列
        1. 定义:可能想要显示并非来自数据源的一列数据,这种列称为未绑定列.
      2. 数据格式示例
        1. 如何:设置 Windows 窗体 DataGridView 控件中的数据格式
          1. DefaultCellStyle
        2. 如何:自定义数据格式
          1. 实现 DataGridView.CellFormatting 事件的处理程序,该处理程序根据单元格的列和值更改单元格的显示方式
      3. 数据验证示例
        1. 验证DataGridView控件中的数据:实现 DataGridView 控件的 CellValidating 和 CellEndEdit 事件的处理程序。如果单元格值验证失败,请将 ValidatingEventArgs 类的 Cancel 属性设置为 true。 这将导致 DataGridView 控件阻止光标离开该单元格。 将该行的 ErrorText 属性设置为解释性字符串。 这将显示错误图标,其工具提示将包含此错误文本。在 CellEndEdit 事件处理程序中,将该行的 ErrorText 属性设置为空字符串。 只有当单元格退出编辑模式(如果验证失败,则不能退出单元格)时,才能发生 CellEndEdit 事件。
        2. 处理DataGridView输入数据时发生的错误:Windows 窗体 DataGridView 控件通过公开 DataError 事件来轻松实现此类错误的处理;当数据存储区检测到约束冲突或违反业务规则时将引发该事件。
      4. 外观自定义示例
        1. 通 过行模板自定义DataGridView中的行:RowTemplate与RowsDefaultCellStyle 属性相比,行模板能更好地控制行的外观和行为。 使用行模板,可以设置包括DefaultCellStyle 在内的任何 DataGridViewRow 属性。
      5. 行为自定义示例
        1. 指定 Windows 窗体 DataGridView 控件的编辑模式
          1. 在编辑模式EditMode中,用户可以更改单元格的值。 也可以防止单元格进入编辑模式,除非调用 BeginEdit 方法。
        2. 为 Windows 窗体 DataGridView 控件中的新行指定默认值
          1. 当应用程序为新添加的行填充默认值时,能使数据输入变得更方便。 通过 DataGridView 类,可以使用 DefaultValuesNeeded 事件填充默认值。 此事件在用户进入新记录的行时引发。 在代码处理此事件时,可以用选择的值填充所需的单元格。
        3. 为DataGridView 控件中的单个单元格添加工具提示
          1. DataGridViewCell.ToolTipText
        4. 使用户能够将多个单元格从DataGridView 控件复制到剪贴板
          1. DataGridView 控件中的数据。
          2. DataGridView.ClipboardCopyMode
          3. 示例
            View Code
      6. 列操作示例
        1. 更改 Windows 窗体 DataGridView列的顺序
          1. DisplayIndex属性
      7. 行和列调整大小示例
        1. 列调整大小

          选中某一列,设置列的AutoSizeMode属性,可以自动调整列的宽度。

      8. 选择示例
        1. 获取选中单元格的数量
          1. dgv_Detail.GetCellCount(DataGridViewElementStates.Selected)
        2. 获取选中行的数量
          1. dgv_Detail.Rows.GetRowCount(DataGridViewElementStates.Selected)
        3. 获取选中列的数量
          1. dgv_Detail.Columns.GetColumnCount(DataGridViewElementStates.Selected)
        4. 判断是否选择了所有的单元格
          1. dgv_Detail.AreAllCellsSelected
      9. 高级自定义示例
      10. 高级数据示例
  2. Windows 窗体 DataGridView 控件中的列类型
    1. DataGridViewTextBoxColumn

      在绑定到数字和字符串时自动生成

    2. DataGridViewCheckBoxColumn

      在绑定到这些类型的值时自动生成

    3. DataGridViewImageColumn

      在绑定到字节数组、Image 对象或 Icon 对象时自动生成

    4. DataGridViewButtonColumn

      通常用作未绑定列。

    5. DataGridViewComboBoxColumn

      通常手动进行数据绑定。

    6. DataGridViewLinkColumn

      通常手动进行数据绑定。

  3. 在 Windows 窗体 DataGridView 控件中显示数据

    待续

  4. 调整 Windows 窗体 DataGridView 控件中列和行的大小

    待续

  5. 对 Windows 窗体 DataGridView 控件中的数据排序
    1. 如何:设置 Windows 窗体 DataGridView 控件中列的排序模式

      SortMode = DataGridViewColumnSortMode.Automatic;

    2. 自定义 Windows 窗体 DataGridView 控件中的排序方式
      1. 以编程方式排序
         1  private void sortButton_Click(object sender, System.EventArgs e)
         2     {
         3         // Check which column is selected, otherwise set NewColumn to null.
         4         DataGridViewColumn newColumn =
         5             dataGridView1.Columns.GetColumnCount(
         6             DataGridViewElementStates.Selected) == 1 ?
         7             dataGridView1.SelectedColumns[0] : null;
         8 
         9         DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
        10         ListSortDirection direction;
        11 
        12         // If oldColumn is null, then the DataGridView is not currently sorted.
        13         if (oldColumn != null)
        14         {
        15             // Sort the same column again, reversing the SortOrder.
        16             if (oldColumn == newColumn &&
        17                 dataGridView1.SortOrder == SortOrder.Ascending)
        18             {
        19                 direction = ListSortDirection.Descending;
        20             }
        21             else
        22             {
        23                 // Sort a new column and remove the old SortGlyph.
        24                 direction = ListSortDirection.Ascending;
        25                 oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
        26             }
        27         }
        28         else
        29         {
        30             direction = ListSortDirection.Ascending;
        31         }
        32 
        33         // If no column has been selected, display an error dialog  box.
        34         if (newColumn == null)
        35         {
        36             MessageBox.Show("Select a single column and try again.",
        37                 "Error: Invalid Selection", MessageBoxButtons.OK,
        38                 MessageBoxIcon.Error);
        39         }
        40         else
        41         {
        42             dataGridView1.Sort(newColumn, direction);
        43             newColumn.HeaderCell.SortGlyphDirection =
        44                 direction == ListSortDirection.Ascending ?
        45                 SortOrder.Ascending : SortOrder.Descending;
        46         }
        47     }
        View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2022-01-15
  • 2021-11-04
猜你喜欢
  • 2022-12-23
  • 2021-06-03
  • 2021-05-23
  • 2022-02-13
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案