编辑
2025-10-31
C#
00

在Windows窗体应用程序开发中,数据绑定是一项核心技术,能够有效地将用户界面与底层数据源连接起来。本文将详细介绍如何在C# Windows Forms应用中实现复杂数据绑定,特别是使用DataGridView控件展示和管理数据。无论你是C#初学者还是希望提升数据处理能力的开发者,本教程都能帮助你掌握这一重要技能。

什么是数据绑定?

数据绑定是指将UI控件与数据源建立连接,使得数据能够自动在两者之间流动。在Windows Forms中,这意味着当数据源发生变化时,UI控件会自动更新;同样,当用户通过UI修改数据时,这些更改也会反映到底层数据源中。

BindingSource类的作用

BindingSource是实现复杂数据绑定的关键组件,它充当UI控件与数据源之间的中介,提供以下优势:

  • 简化数据源与控件之间的连接
  • 支持数据筛选和排序
  • 提供内置的导航功能
  • 处理数据变更通知
  • 简化多控件共享同一数据源的实现

实战案例:员工管理系统

下面,我们将通过一个员工管理系统的案例,展示如何实现复杂数据绑定。

第一步:定义数据模型

首先,我们需要创建一个代表员工的数据模型类:

C#
// 员工数据模型 public class Employee { public int Id { get; set; } // 员工ID public string Name { get; set; } // 姓名 public string Department { get; set; } // 所属部门 public decimal Salary { get; set; } // 薪资 public DateTime HireDate { get; set; } // 入职日期 public bool IsActive { get; set; } // 在职状态 }

第二步:初始化数据源

接下来,我们需要创建一个数据源,在实际应用中这通常来自数据库,但在本例中我们使用模拟数据:

C#
// 初始化员工数据 private void InitializeDataSource() { // 模拟数据库数据 employeeList = new List<Employee> { new Employee { Id = 1, Name = "张三", Department = "技术部", Salary = 8000, HireDate = DateTime.Now.AddYears(-3), IsActive = true }, new Employee { Id = 2, Name = "李四", Department = "市场部", Salary = 7000, HireDate = DateTime.Now.AddYears(-2), IsActive = true }, new Employee { Id = 3, Name = "王五", Department = "人事部", Salary = 6000, HireDate = DateTime.Now.AddYears(-1), IsActive = false } }; }

第三步:配置DataGridView并绑定数据

最后,我们将数据源与DataGridView控件绑定,并自定义显示效果:

C#
// 配置DataGridView并绑定数据 private void SetupDataGridView() { // 创建绑定源作为UI与数据之间的桥梁 bindingSource = new BindingSource(); bindingSource.DataSource = employeeList; // 将绑定源关联到DataGridView dataGridView1.DataSource = bindingSource; // 自定义列显示 dataGridView1.Columns["Id"].Visible = false; // 隐藏ID列 dataGridView1.Columns["Name"].HeaderText = "姓名"; dataGridView1.Columns["Department"].HeaderText = "部门"; dataGridView1.Columns["Salary"].HeaderText = "薪资"; dataGridView1.Columns["HireDate"].HeaderText = "入职日期"; dataGridView1.Columns["IsActive"].HeaderText = "在职状态"; }
编辑
2025-10-31
C#
00

DataGridView作为Windows窗体应用程序中最常用的数据展示控件,其灵活的单元格渲染机制为开发者提供了无限可能。本文将深入剖析DataGridView单元格渲染的核心技术,帮助开发者解锁自定义单元格渲染的艺术。

单元格渲染基础详解

基础渲染类继承

C#
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace AppDataGrid { public class CustomTextCell : DataGridViewTextBoxCell { protected override void Paint( Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { // 修改:我们需要绘制除了文本之外的所有部分(背景、边框等) // 通过排除文本部分,避免基类绘制文本造成重叠 DataGridViewPaintParts partsWithoutText = paintParts & ~DataGridViewPaintParts.ContentForeground; // 调用基类渲染方法,但不包括文本部分 base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, partsWithoutText); // 只有当需要绘制内容前景(文本)时才进行自定义文本绘制 if ((paintParts & DataGridViewPaintParts.ContentForeground) != 0 && value != null) { // 抗锯齿绘制 graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; // 自定义文本样式 using (SolidBrush brush = new SolidBrush(Color.Navy)) using (Font customFont = new Font("微软雅黑", 9, FontStyle.Bold)) { // 创建适合文本对齐的StringFormat StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; // 调整文本绘制区域,留出边距 Rectangle textRect = new Rectangle( cellBounds.X + 2, cellBounds.Y + 2, cellBounds.Width - 4, cellBounds.Height - 4); graphics.DrawString( value.ToString(), customFont, brush, textRect, stringFormat ); } } } // 重写Clone方法以确保正确复制单元格 public override object Clone() { return new CustomTextCell(); } // 确保默认新行单元格值正确 public override object DefaultNewRowValue => string.Empty; // ====== 修复编辑功能 ====== // 使用标准的TextBox作为编辑控件 public override Type EditType => typeof(DataGridViewTextBoxEditingControl); // 设置值类型 public override Type ValueType => typeof(string); // 正确处理准备编辑状态 public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) { // 调用基类方法确保正确初始化编辑控件 base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); // 获取并配置编辑控件 if (DataGridView.EditingControl is DataGridViewTextBoxEditingControl textBox) { // 如果单元格值不为空,则设置文本框内容 textBox.Text = (Value == null) ? string.Empty : Value.ToString(); // 可选:配置文本框的其他属性,比如字体等 textBox.Font = new Font("微软雅黑", 9, FontStyle.Regular); } } // 确保可以编辑 public override bool ReadOnly { get { return base.ReadOnly; } set { base.ReadOnly = value; } } } // 创建自定义列类型以使用自定义单元格 public class CustomTextColumn : DataGridViewColumn { public CustomTextColumn() : base(new CustomTextCell()) { // 设置列的默认属性 SortMode = DataGridViewColumnSortMode.Automatic; DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } public override DataGridViewCell CellTemplate { get { return base.CellTemplate; } set { // 确保始终使用自定义单元格类型 if (value != null && !value.GetType().IsAssignableFrom(typeof(CustomTextCell))) { throw new InvalidCastException("必须使用CustomTextCell类型的单元格"); } base.CellTemplate = value; } } } }

image.png

编辑
2025-10-31
C#
00

在现代软件开发中,数据库的迁移是一项常见的任务,尤其是在项目需要从MySQL迁移到SQL Server时。本文将详细介绍一个使用C#编写的数据库迁移工具,它的主要功能是将MySQL数据库中的表结构和数据迁移到SQL Server中。该工具支持获取MySQL表的详细架构信息、创建SQL Server表及其主键和注释支持,并批量迁移数据。

代码实现

Nuget 安装MySql.Data

image.png

编辑
2025-10-31
C#
00

本文将详细讲解C# Winform中DataGridView控件的基础应用,通过实际代码示例帮助开发者快速掌握DataGridView的使用技巧。

什么是DataGridView?

DataGridView是Windows窗体应用程序中最常用的数据展示控件,它可以以表格形式灵活地显示和编辑数据。主要由行(Rows)、列(Columns)和单元格(Cells)组成。

DataGridView基本使用

创建DataGridView控件

C#
// 在Form设计器中添加DataGridView控件 private DataGridView dataGridView1; // 代码初始化方式 private void InitializeDataGridView() { dataGridView1 = new DataGridView(); this.Controls.Add(dataGridView1); dataGridView1.Dock = DockStyle.Fill; // 填充整个窗体 }

手动添加列和数据

C#
private void PopulateDataGridView() { // 添加列 dataGridView1.Columns.Add("ID", "编号"); dataGridView1.Columns.Add("Name", "姓名"); dataGridView1.Columns.Add("Age", "年龄"); // 添加数据行 dataGridView1.Rows.Add(1, "张三", 25); dataGridView1.Rows.Add(2, "李四", 30); }

数据绑定

C#
// 使用数据集绑定 private void BindDataGridView() { // 假设有一个DataTable类型的数据源 DataTable dt = GetDataFromDatabase(); dataGridView1.DataSource = dt; }
编辑
2025-10-31
C#
00

在Windows Forms应用程序中,系统默认的TextBox控件功能相对简单,且样式单一。为了满足现代UI设计的需求,我们需要一个更加灵活、可定制的输入控件。本文将详细介绍如何通过继承UserControl和内置TextBox来创建一个增强版本的文本输入控件BzTextBox

控件特性概览

我们的BzTextBox控件具备以下高级特性:

  • 可自定义边框颜色和大小
  • 支持圆角边框
  • 提供下划线样式
  • 内置占位符文本
  • 密码模式
  • 多行文本支持
  • 灵活的样式属性

关键实现细节

边框绘制

控件最大的亮点在于其自定义边框绘制逻辑。通过重写OnPaint方法,我们实现了多种边框样式:

C#
protected override void OnPaint(PaintEventArgs e) { // 支持圆角边框 if (borderRadius > 1) { // 复杂的圆角路径绘制逻辑 using (GraphicsPath pathBorderSmooth = GetFigurePath(rectBorderSmooth, borderRadius)) { // 边框绘制 } } else { // 普通矩形边框绘制 } }