我有一个通过绑定源将 datagridview 绑定到数据表的代码:
_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 只是一个空字符串。
你能告诉我我做错了什么,以及如何格式化我的单元格。谢谢!