【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