【发布时间】:2011-04-14 03:43:58
【问题描述】:
我有一个通过绑定源将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 只是一个空字符串。
您能否告知我做错了什么,以及如何格式化我的单元格。 谢谢!
【问题讨论】:
标签: c# data-binding datagridview cell-formatting