且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

【Winform】单元格的Formatted值的类型错误

更新时间:2022-09-17 09:25:26

最近在做一个C# winform应用程序,第一次接触C# winform开发,觉得还真不习惯,很多东西不知如何着手,与asp.net相差还是比较大的。就如今天遇到的一个问题,想将DataGridView的某一列格式化一下,就出现问题了:

 

DataGridView中发生以下异常:
System.FormatException:单元格的Formatted值的类型错误。
要替换此默认对话框,请处理DataError事件。

最后经查找将代码更正后即没事了:

 

【Winform】单元格的Formatted值的类型错误
 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}",
            
//          "好啊");
            
//        }
            
//    }
            
//}
        }
【Winform】单元格的Formatted值的类型错误

 

转载请注明出处[http://samlin.cnblogs.com/] 
作者赞赏
【Winform】单元格的Formatted值的类型错误 【Winform】单元格的Formatted值的类型错误


刚做的招标网:八爪鱼招标网 请大家多意见
标签: winform

本文转自Sam Lin博客博客园博客,原文链接:http://www.cnblogs.com/samlin/archive/2012/03/28/FormatException.html,如需转载请自行联系原作者