如何从gridview中显示错误行?

时间:2011-08-27 15:26:19

标签: c# asp.net gridview

我有这些代码用于上传csv文件。另外,我已经放置了一个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。如果是这样,有人能告诉我代码吗?

1 个答案:

答案 0 :(得分: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"); } } } 事件的缺失值。

示例:

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

不要忘记在gridview属性中声明它:

{{1}}