如何在GridView中检查和显示空列或行?

时间:2011-08-29 02:39:34

标签: c# asp.net gridview

我有这个代码允许用户上传csv文件,内容将显示在GridView中,问题是,如果任何行/列为空,我需要显示错误信息,任何人都可以帮助我有了这个?我不确定使用asp.net c#cos我是新手。这是我的代码:

    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();











    }

请帮忙!

1 个答案:

答案 0 :(得分:1)

创建一个名为value或error message的方法,并将列[index]传递给

.Select(columns => new {GuestName = ValueOrErrorMessage(columns[0]), Guest_ID = ValueOrErrorMessage(columns[1]), IC_Number = ValueOrErrorMessage(columns[2])}); 
      ...

private string ValueOrErrorMessage(string input){
   if(!string.IsNullOrEmpty(input))
      return input;
   }
   return "no value"
}