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;
}
}
}
}

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

本文将详细讲解C# Winform中DataGridView控件的基础应用,通过实际代码示例帮助开发者快速掌握DataGridView的使用技巧。
DataGridView是Windows窗体应用程序中最常用的数据展示控件,它可以以表格形式灵活地显示和编辑数据。主要由行(Rows)、列(Columns)和单元格(Cells)组成。
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;
}
在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
{
// 普通矩形边框绘制
}
}
在 Windows 桌面应用开发中,剪贴板是一个非常有用的功能,它允许用户在不同应用程序之间复制和粘贴数据。在 WinForms 应用程序中,我们可以通过调用 Win32 API 来实现剪贴板操作。本文将详细介绍如何在 WinForms 中进行剪贴板操作,并提供一个完整的示例。
在开始之前,我们需要引用一些必要的命名空间:
C#using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
我们将使用一些 Win32 API 函数来访问剪贴板,这些函数包括:
OpenClipboardCloseClipboardEmptyClipboardSetClipboardDataGetClipboardDataGlobalAllocGlobalLockGlobalUnlock在代码中声明这些 API 函数:
C#public class ClipboardAPI
{
// 声明 Win32 API 函数
[DllImport("user32.dll")]
public static extern bool OpenClipboard(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool CloseClipboard();
[DllImport("user32.dll")]
public static extern bool EmptyClipboard();
[DllImport("user32.dll")]
public static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll")]
public static extern IntPtr GetClipboardData(uint uFormat);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("kernel32.dll")]
public static extern bool GlobalUnlock(IntPtr hMem);
// 定义数据格式
public const uint CF_TEXT = 1;
}