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

本文关键字:显示 检查和 GridView | 更新日期: 2023-09-27 18:06:48

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

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





    }

请帮忙!

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

创建一个名为value or 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"
}