最近在做一個C# winform應用程序,第一次接觸C# winform開發,覺得還真不習慣,很多東西不知如何着手,與asp.net相差還是比較大的。就如今天遇到的一個問題,想將DataGridView的某一列格式化一下,就出現問題了:
DataGridView中發生以下異常:
System.FormatException:單元格的Formatted值的類型錯誤。
要替換此默認對話框,請處理DataError事件。
System.FormatException:單元格的Formatted值的類型錯誤。
要替換此默認對話框,請處理DataError事件。
最后經查找將代碼更正后即沒事了:
private
void gvList_CellFormatting(
object sender, DataGridViewCellFormattingEventArgs e)
{
if (gvList.Rows[e.RowIndex].IsNewRow)
return;
if (gvList.Columns[e.ColumnIndex].Name == " StreetID ")
{
if (e.Value == null)
e.Value = string.Empty;
else {
// e.Value = "本街道";
int streedId = Utils.ConvertToInt32(e.Value.ToString());
if (streedId > 0)
{
Street streetModel = Utils.GetStreetModel(streedId);
if (streetModel != null)
e.Value = streetModel.Name;
}
}
}
if (e.ColumnIndex == 0)
{
e.Value = e.RowIndex + 1; //DataGridView行號,序號
}
// if (e.ColumnIndex == 2) {
// // e.FormattingApplied = true;
// DataGridViewRow row =gvList.Rows[e.RowIndex];
// if(row!=null){
// if (row.Cells[2].Value != null && row.Cells[3].Value.ToString() == "2")
// {
// e.Value = string.Format("{0}",
// "好啊");
// }
// }
// }
}
{
if (gvList.Rows[e.RowIndex].IsNewRow)
return;
if (gvList.Columns[e.ColumnIndex].Name == " StreetID ")
{
if (e.Value == null)
e.Value = string.Empty;
else {
// e.Value = "本街道";
int streedId = Utils.ConvertToInt32(e.Value.ToString());
if (streedId > 0)
{
Street streetModel = Utils.GetStreetModel(streedId);
if (streetModel != null)
e.Value = streetModel.Name;
}
}
}
if (e.ColumnIndex == 0)
{
e.Value = e.RowIndex + 1; //DataGridView行號,序號
}
// if (e.ColumnIndex == 2) {
// // e.FormattingApplied = true;
// DataGridViewRow row =gvList.Rows[e.RowIndex];
// if(row!=null){
// if (row.Cells[2].Value != null && row.Cells[3].Value.ToString() == "2")
// {
// e.Value = string.Format("{0}",
// "好啊");
// }
// }
// }
}