C# 绑定DataGridView时不应用DefaultCellStyle格式
我有一个通过绑定源将datagridview绑定到datatable的代码:C# 绑定DataGridView时不应用DefaultCellStyle格式,c#,data-binding,datagridview,cell-formatting,C#,Data Binding,Datagridview,Cell Formatting,我有一个通过绑定源将datagridview绑定到datatable的代码: _bind.DataSource = Table; lGrid.DataSource = _bind; 在DataBindingComplete事件中,我为某些列设置了DefaultCellStyle: void lGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { if (e.ListChange
_bind.DataSource = Table;
lGrid.DataSource = _bind;
在DataBindingComplete事件中,我为某些列设置了DefaultCellStyle:
void lGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (e.ListChangedType == ListChangedType.Reset)
lGrid.Columns[0].DefaultCellStyle.Format = "+#,##0;-#,##0;0";
}
此时,桌子仍然是空的。因此,稍后将行添加到表中并在DataGridView中很好地反映时,由于某些原因,该格式不会应用于列0的单元格
我检查了CellFormatting事件中的格式:
private void lGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs args)
{
if (args.ColumnIndex == lGrid.Columns[0].Index)
{
string s1 = lGrid.Columns[0].DefaultCellStyle.Format;
string s2 = lGrid[0, args.RowIndex].Style.Format;
}
}
s1返回一个正确的格式,但是s2只是一个空字符串
你能告诉我我做错了什么,以及如何格式化我的手机吗。
谢谢 尝试格式化事件中的单元格样式
尝试格式化事件中的单元格样式
这可能是因为DataTable列datatype设置为string(默认情况下) 因此,您可以将DataTable Columns数据类型更改为numeric:
Table.Columns.Add(ColumnName,typeof(double));
Table.Columns[0].DataType = typeof(decimal);
这可能是因为DataTable列datatype设置为string(默认情况下) 因此,您可以将DataTable Columns数据类型更改为numeric:
Table.Columns.Add(ColumnName,typeof(double));
Table.Columns[0].DataType = typeof(decimal);
我尝试在CellFormatting中设置lGrid[0,args.RowIndex].Style.Format=“+#,##0;-#,##0;0”,但它没有work@jfs是的,它在数据网格上工作,数据网格不绑定到任何表,也不直接更新。@jfs感谢您的建议,不幸的是,这不起作用。事实上,当我在执行赋值args.CellStyle.Format=“+#,##0;-#,##0;0”之前选中args.CellStyle.Format时,它实际上已经等于它了。但是仍然没有显示在DataGridView上。@Anya您能粘贴更新的代码吗?格式化后,
args.value
的值是多少?我尝试在CellFormatting中设置lGrid[0,args.RowIndex].Style.Format=“+#,##0;-#,##0;0”,但没有work@jfs是的,它在数据网格上工作,数据网格不绑定到任何表,也不直接更新。@jfs感谢您的建议,不幸的是,这不起作用。事实上,当我在执行赋值args.CellStyle.Format=“+#,##0;-#,##0;0”之前选中args.CellStyle.Format时,它实际上已经等于它了。但是仍然没有显示在DataGridView上。@Anya您能粘贴更新的代码吗?格式化后,args.value
的值是多少?这是我的问题。但我使用的是designer,将dgv绑定到数据源。。因此,只需在Designer中转到数据集的DataTable,选择列,然后在properties窗口中,将数据类型设置为System.Double这是我的问题。但我使用的是designer,将dgv绑定到数据源。。因此,只需在Designer中转到数据集的DataTable,选择列,然后在properties窗口中,将数据类型设置为System.Double