Han*_*man 4 c# datagridview winforms
我有一个绑定到DataTable的DataGridView。
DataTable是所有数值。
要求DataGridView中的每n行中都包含文本,而不是数字值(以便为用户在视觉上分隔各个部分)。
绑定后,我很乐意将此文本数据放入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;
}
}
//...
}
Run Code Online (Sandbox Code Playgroud)
有关创建自己的样式的帮助,请参阅DataGridView.CellFormatting Event
在线帮助中的主题。