【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}",
            
//           "好啊");
            
//         }
            
//     }
            
// }
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM