我有一个DataGridView绑定到DataTable。 DataTable是一个全数字值。要求DataGridView中的每n行都有文本,而不是数字值(以便为用户直观地分隔各个部分)。我很高兴在绑定后将这些文本数据放入DataTable或DataGridView中,但我无法看到将这些文本数据放入其中的方式,因为两种格式都需要数字数据 - 我得到“can not把一个字符串放在一个小数点“的错误。任何想法如何改变DataTable或DataGridView中特定行或单元格的格式?我如何在Winform DataGridView中创建不同的单元格格式#
4
A
回答
4
您可以在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 Event
主题联机帮助中
1
我已将Janus GridEx用于此类非标准行为。 (部分原因是它不仅仅是这样做,比如分组和总结,卡片视图等)。网站上有一些很好的演示。
(不是推销。我只是使用其组件的良好体验)
1
这是否解决问题了吗?
// Set the data source.
dataGridView1.DataSource = dataTable1;
// Create a new text box column.
DataGridViewColumn c1 = new DataGridViewTextBoxColumn();
const string C1_COL_NAME = "Custom1";
c1.Name = C1_COL_NAME;
// Insert the new column where needed.
dataGridView1.Columns.Insert(1, c1);
// Text can then be placed in the rows of the new column.
dataGridView1.Rows[0].Cells[C1_COL_NAME].Value = "Some text...";
原始数据表绑定应该仍然存在。
相关问题
- 1. 编辑模式下datagridview单元格的格式不同
- 2. 在Datagridview中格式化单元格
- 3. 如何在DataGridView单元格中设置格式掩码?
- 4. DataGridView - “单元格选择样式” - 编辑单元格
- 5. DataGridView单元格
- 6. 如何在DataGridView中编辑单元格?
- 7. Datagridview保持单元格格式
- 8. 如何在JGraphX中为特定单元格创建不同的边线样式?
- 9. 如何验证DataGridView中的单元格?
- 10. 如何更新datagridview中的单元格?
- 11. WPF DataGrid的单元格样式 - 同一列中的不同单元格样式
- 12. 使用C#在winform中datagridview合并单元格?
- 13. 使用不同大小的单元格创建表格
- 14. 创建不同宽度单元格的滚动网格
- 15. 如何为DataGridView中的不同单元格设置不同的ContextMenu
- 16. 从C#winforms中的不同线程更新DataGridView单元格
- 17. 如何使用c#winform从datagridview中编辑的单元格中获取数据?
- 18. 如何创建偏移公式以使用单元格中的引用(公式指向的单元格),而不是公式所在的单元格地址?
- 19. 通过单击c#(winform)更改datagridview单元格颜色
- 20. 如何在iOS中创建多行表格单元格?
- 21. 如何在单元格中创建表格变量?
- 22. 如何在表格单元格中创建斜线
- 23. 如何以编程方式在每行中以不同数量的单元格创建表格?
- 24. 创建一个固定格式的单元格中的UITableView
- 25. 格式化DataGridView单元格而不更改基础值?
- 26. 访问单元格datagridview的内容Winform C#
- 27. 如何在每个单元格中使用不同的DataSource设置DataGridView ComboBoxColumn?
- 28. Winform在RichTextBox控件中创建表格
- 29. 如何在tableview中创建动态单元格(没有自定义单元格)?
- 30. 更改datagridview单元格中编辑的单元格
所以你不认为我的要求可以满足标准的DataGridView? – Handleman 2009-02-26 22:50:42