【WinForm详细教程七】WinForm中的DataGridView控件

编程入门 行业动态 更新时间:2024-10-25 02:23:10

【WinForm详细教程七】WinForm中的DataGridView<a href=https://www.elefans.com/category/jswz/34/1769529.html style=控件"/>

【WinForm详细教程七】WinForm中的DataGridView控件

文章目录

      • 1.主要属性
        • DataSource
        • 行(Row 相关属性)
        • 列(Column 相关属性)
        • 单元格(Cell 相关属性)
        • 逻辑删除
        • AllowUserToAddRows
        • AllowUserToDeleteRows
        • AllowUserToOrderColumns
        • 其他布局和行为属性
      • 2.控件中的行、列和单元格类
        • DataGridViewColumn 类的属性
        • DataGridViewRow 类的属性
        • DataGridViewCell 类的属性
      • 3.一个简单的示例,连接SQL数据库示例
      • 4.DataGridView 数据源
        • 绑定方式
        • 优化建议

DataGridView 是一种在Windows窗体应用程序中使用的控件,它以网格(表格)的形式显示数据。这种控件允许用户定义行和列,以及对应的单元格。核心概念:

  • 行(Rows):数据呈现的横向序列。
  • 列(Columns):数据呈现的纵向序列。
  • 单元格(Cells):行和列交叉点的数据存储单位。

1.主要属性

DataSource

DataSource 属性用于设置 DataGridView 控件的数据源。常用的数据源类型有:

  • DataTable:从数据库查询得到的数据表。
  • List<T>:泛型集合,其中 T 是数据模型的类型。
行(Row 相关属性)
  • DataGridViewRow: 表示 DataGridView 控件中的一行。
  • DataGridViewRowCollection: 表示 DataGridView 控件中所有行的集合。
  • Rows: 通过此属性可以访问或操作行集合中的行。
列(Column 相关属性)
  • DataGridViewColumn: 表示 DataGridView 控件中的一列。
  • DataGridViewColumnCollection: 表示控件中所有列的集合。
  • Columns: 通过此属性可以访问或操作列集合中的列。
单元格(Cell 相关属性)
  • DataGridViewCell: 表示 DataGridView 控件中的一个单元格。
  • Value: 单元格存储的实际数据。
  • Selected: 表示单元格是否被选中。
  • RowIndex: 单元格所在的行索引。
  • ColumnIndex: 单元格所在的列索引。
  • FormattedValue: 用于显示的单元格经过格式化后的值。
逻辑删除

在某些业务场景下,行记录不是真正从数据源中删除,而是通过修改标识列的值来表示记录的状态(例如,0 表示正常,1 表示已删除)。

AllowUserToAddRows

控制是否显示用于添加新行的空白行。

  • True: 显示空白行,并且在用户输入数据时自动添加新的空白行。输入完成后按回车,数据就添加到控件中。
  • False: 不显示用于添加新行的空白行。
AllowUserToDeleteRows

控制用户是否可以从 DataGridView 中删除行,默认值为 true。用户可以通过选中行并按 Delete 键来删除行。

AllowUserToOrderColumns

控制是否允许用户手动调整列的位置。

  • True: 允许用户通过拖动来重新放置列。
  • False: 不允许用户手动调整列的位置。
其他布局和行为属性
  • AllowUserToResizeColumns: 是否允许用户调整列宽。
  • AllowUserToResizeRows: 是否允许用户调整行高。
  • AutoSizeColumnsMode: 确定列的自动大小调整模式。
  • AutoSizeRowsMode: 确定行的自动大小调整模式。
  • EditMode: 定义何时可以编辑单元格的内容。
  • GridColor: 网格线的颜色。
  • MultiSelect: 是否允许用户同时选择多个单元格、行或列。
  • SelectionMode: 设置单元格选择模式。

2.控件中的行、列和单元格类

DataGridViewColumn 类的属性
  • Name: 列的名称。
  • ColumnType: 列的类型,例如 DataGridViewTextBoxColumn, DataGridViewCheckBoxColumn 等。
  • DataPropertyName: 绑定到数据源的属性名。
  • HeaderText: 列标题显示的文本。
DataGridViewRow 类的属性
  • DataBoundItem: 绑定行的数据对象。
  • Selected: 行是否被选中。
  • State: 行的状态,如 DataGridViewElementStates
  • Cells: 行的单元格集合。
DataGridViewCell 类的属性
  • Value: 单元格的实际值。
  • Selected: 是否被选中。
  • RowIndex: 所在行的索引。
  • OwningRow: 所属的行。
  • OwningColumn: 所属的列。
  • ColumnIndex: 所在列的索引。
  • FormattedValue: 显示格式化后的值。
  • FormattedValueType: 格式化值的类型。
  • ValueType: 单元格值的数据类型。

3.一个简单的示例,连接SQL数据库示例

using System.Data;
using WinFormsTest.Helper;namespace WinFormsTest
{public partial class frmDataGridView : Form{public frmDataGridView(){InitializeComponent();}private void frmDataGridView_Load(object sender, EventArgs e){string sql = "select * from UserInfo";DataTable dt = DBHelper.GetDataTable(sql, 1);dataGridView1.AutoGenerateColumns = false;dataGridView1.DataSource = dt;}private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];string nameUser = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();if (cell.FormattedValue.ToString() == "删除"){MessageBox.Show("删除了:" + nameUser);}else if (cell.FormattedValue.ToString() == "修改"){MessageBox.Show("修改了:" + nameUser);}}}
}

4.DataGridView 数据源

  • DataGridView可以绑定不同类型的数据源,包括DataTableList<T>BindingList<T>等。
  • 绑定到List<T>时,每个T的实例对应DataGridView的一行。
  • DataBoundItem属性返回当前行绑定的对象,例如,DataGridViewRow.DataBoundItem将返回绑定列表中的相应对象。
绑定方式
  1. DataTable:
    • 使用DataTable作为数据源时,每行数据通过DataRow对象表示。
    • 对于大量数据,DataTable可能是较好的选择,因为它内置了很多对数据操作的支持。
  2. List:
    • 当使用List<T>作为数据源时,数据以对象列表的形式存在。
    • 对于处理实体对象集合,尤其是当数据量不大的情况下,List<T>是一个合适的选择。
优化建议
  • 当数据量大时,推荐使用DataTable,因为它被设计用来处理大批量数据。
  • 对于小量数据或者需要表示复杂对象时,List<T>更合适,因为它可以直接与对象的属性相绑定。
  • SqlDataReader一次只读取一行数据,并且是只进的,适合大批量数据读取,但在Windows Forms中,直接将SqlDataReader的数据转换为List<T>后再绑定到DataGridView会更加灵活。

精彩推荐:
【C#进阶一】C#中的数组(Array)、集合(ArrayList,Queue,Stack, HashList)、List<T>、字典(Dictionary<K,T>)和双向链表LinkedList
【C#进阶八】C#中的序列化与反序列化下(二进制序列化、XML序列化及JSON序列化)

【C#进阶】C#语法中一些常用知识点总结
【WinForm详细教程一】WinForm中的窗体、Label、TextBox及Button控件、RadioButton和CheckBox、ListBox
【WinForm详细教程三】WinForm中的NumericUpDown、PictureBox、RichTextBox及三种Timer控件
【WinForm详细教程四】WinForm中的ProgressBar 、ImageList和ListView控件
【WinForm详细教程五】WinForm中的MenuStrip 、ContextMenuStrip 、ToolStrip、StatusStrip控件
【WinForm详细教程六】WinForm中的GroupBox和Panel 、TabControl 、SplitContainer控件
【C#进阶】C#中的委托、事件、回调函数、匿名函数和lambda表达式
希望有所帮助,同时欢迎关注我,后面将更新更多相关内容!

更多推荐

【WinForm详细教程七】WinForm中的DataGridView控件

本文发布于:2023-11-16 16:45:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   教程   详细   WinForm   DataGridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!