2011-08-27 55 views
0

我有這些代碼上傳CSV文件。另外,我已經放置了一個GridView來顯示它的內容。現在,我想要做的就是顯示來自GridView的錯誤行,例如,如果在特定列或行中缺少單詞,我想要顯示該特定行是空的,並且用戶必須重新上傳更新了該特定行。事情是,我真的很新,希望有人能幫助我!我不知道如何檢索錯誤行並將其顯示在網站上。這些是我的代碼:如何顯示gridview中的錯誤行?

protected void btnUpload_Click(object sender, EventArgs e) 
    { 
     string strFileNameOnServer = fileUpload.PostedFile.FileName; 
     string fileExt = 
     System.IO.Path.GetExtension(fileUpload.FileName); 


     if (fileUpload.PostedFile != null && fileExt == ".csv") 
     { 

      try 
      { 

       //fileUpload.PostedFile.SaveAs(ConfigurationManager.AppSettings + appDataPath + "\\" + strFileNameOnServer); 
       fileUpload.PostedFile.SaveAs(Server.MapPath("~/Uploaded")); 
       //string appPath = HttpContext.Current.Request.ApplicationPath; 
       // string physicalPath = HttpContext.Current.Request.MapPath("~/MajorProject"); 
       Label1.Text = "File name: " + 
         fileUpload.PostedFile.FileName + "<br>" + 
         fileUpload.PostedFile.ContentLength + " kb<br>" + 
         "Content type: " + 
         fileUpload.PostedFile.ContentType; 
      } 
      catch (Exception ex) 
      { 
       Label1.Text = "Error saving <b>" + strFileNameOnServer + "</b><br>. " + ex.Message; 
      } 
      BtnImport1.Visible = true; 
      Cancel.Visible = true; 
      fileUpload.Visible = false; 
      btnUpload.Visible = false; 
     } 
     else 
     { 

      Label1.Text = "Error - a file name must be specified/only csv files are allowed"; 
      return; 

     } 


     var data = File.ReadAllLines(Server.MapPath("~/Uploaded")) 
      .Select(line => line.Split(','))  
      .Select(columns => new {GuestName = columns[0], Guest_ID = columns[1], IC_Number = columns[2]}); 
     myGridView.DataSource = data; 
     myGridView.DataBind(); 

     } 

是否必須將新項目添加到我的aspx文件中?就像一個顯示錯誤行的標籤一樣? csv文件的內容有Name,Ic Number和House。如果是這樣,任何人都可以顯示我的代碼來做到這一點?

+0

您上傳CSV文件的事實是不相關的問題。你的問題是如何在GridView中的單元格中指示錯誤。 –

+0

@約翰桑德斯,哦!對不起,我錯誤地格式化了這個問題,將其改爲 – Mark20

回答

0

您可以在GridView.RowDataBound事件myGridview上檢查缺失值。

例子:

void myGridView_RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 

    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // Check if any of the values of mygridview row cells are empty. 
     if (e.Row.Cells[0].Text == "") 
     { 
      e.Row.Cells[0].Text = "ERROR: Guest name is empty!" 
      e.Row.Cells[0].Attributes.CssStyle.Add(HtmlTextWriterStyle.Color, "red"); 
     } 
     if (e.Row.Cells[1].Text == "") 
     { 
      e.Row.Cells[1].Text = "ERROR: Guest Id is empty!" 
      e.Row.Cells[1].Attributes.CssStyle.Add(HtmlTextWriterStyle.Color, "red"); 
     } 
     if (e.Row.Cells[2].Text == "") 
     { 
      e.Row.Cells[2].Text = "ERROR: Ic number is empty!" 
      e.Row.Cells[2].Attributes.CssStyle.Add(HtmlTextWriterStyle.Color, "red"); 
     } 


    } 

    } 

不要忘了將它聲明GridView控件的屬性裏面:

<asp:GridView ID="myGridview" 
       runat="server" 
       OnRowDataBound="myGridView_RowDataBound"> 
</asp:GridView> 
+0

謝謝!我會在tmrw到達我的工作場所時嘗試一下,非常感謝! – Mark20