gpt4 book ai didi

c# - 如何在 Winform DataGridView 中创建不同的单元格格式

转载 作者:太空狗 更新时间:2023-10-30 00:47:28 25 4
gpt4 key购买 nike

我有一个要绑定(bind)到 DataTable 的 DataGridView。
DataTable是一个全数字值。
要求 DataGridView 中的每 n 行都包含文本,而不是数值(以便在视觉上为用户分隔部分)。

我很乐意在绑定(bind)后将此文本数据放入 DataTable 或 DataGridView 中,但我看不到将此文本数据放入其中的方法,因为两者的列格式都需要数字数据 - 我得到一个“不能将字符串放入小数中”这两个错误。

关于如何更改 DataTable 或 DataGridView 中特定行或单元格的格式的任何想法?

最佳答案

您可以为 DataGridView 的 CellFormatting 事件提供处理程序,例如:

public partial class Form1 : Form
{
DataGridViewCellStyle _myStyle = new DataGridViewCellStyle();

public Form1()
{
InitializeComponent();

_myStyle.BackColor = Color.Pink;
// We could also provide a custom format string here
// with the _myStyle.Format property
}

private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
// Every five rows I want my custom format instead of the default
if (e.RowIndex % 5 == 0)
{
e.CellStyle = _myStyle;
e.FormattingApplied = true;
}
}

//...
}

有关创建您自己的样式的帮助,请参阅在线帮助中的 DataGridView.CellFormatting 事件主题。

关于c# - 如何在 Winform DataGridView 中创建不同的单元格格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/592757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com