zoukankan      html  css  js  c++  java
  • 【Winform】单元格的Formatted值的类型错误

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

    DataGridView中发生以下异常:
    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}",
                
    //          "好啊");
                
    //        }
                
    //    }
                
    //}
            }
    转载请注明出处[http://samlin.cnblogs.com/

    欢迎关注本人公众号:

    作者赞赏
  • 相关阅读:
    UOJ309 UNR #2 排兵布阵
    BZOJ4860: [Beijing2017]树的难题
    CQOI2017 部分题解
    SDOI2017 Round1 Day2 题解
    记SCOI2017
    BZOJ3810: [Coci2015]Stanovi
    BZOJ4785: [Zjoi2017]树状数组
    「ZJOI2007」「LuoguP1169」棋盘制作(并查集
    「LuoguP4147」 玉蟾宫(并查集
    「LuoguP1402」 酒店之王(最大流
  • 原文地址:https://www.cnblogs.com/samlin/p/FormatException.html
Copyright © 2011-2022 走看看