在Gridview中检查重复项并显示错误消息(如果有)?

时间:2011-09-06 23:12:48

标签: c# asp.net gridview error-handling

我有一个上传功能,可以将csv文件上传到gridview。我想检查Gridview的第一列中是否有任何重复的记录记录。如果有,我想显示错误消息,如“重复记录!”并使一个名为btnimport1的按钮不可见。谁能帮我?到目前为止,这是我的代码:

    //A method to display errors in the gridview
    private string ErrorMessage(string input)

        {
            {
                //if there are null values, error message will be displayed.
                if (!string.IsNullOrEmpty(input))
                    return input;
                //making the button invisble, so that the user will be forced to cancel import and re-upload new file
                BtnImport1.Visible = false;
            }
            return "No value entered!";

        }

   protected void btnUpload_Click(object sender, EventArgs e)
    {

        //get the uploaded file name
        string strFileNameOnServer = fileUpload.PostedFile.FileName;
        //get the uploaded file's extension
        string fileExt =
        System.IO.Path.GetExtension(fileUpload.FileName);



        // if the uploaded file is not null and the file extension is csv, do the try
        if (fileUpload.PostedFile != null && fileExt == ".csv")
        {

            try
            {


                fileUpload.PostedFile.SaveAs(Server.MapPath("~/Uploads"));

                //to display the contents of file
                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;
            }

            //to make the import and cancel import button visible so that users can either choose to import or cancel
            BtnImport1.Visible = true;
            Cancel.Visible = true;

            //to make the upload button invisible
            fileUpload.Visible = false;
            btnUpload.Visible = false;

        }
        else
        {
            //if the user does not select anything or the file is extension is not .csv, the error message will be displayed
            Label1.Text = "Error - a file name must be specified/only csv files are allowed";
            return;

        }

        // to read all lines of the posted csv file and put the lines in the grid view
        var data = File.ReadAllLines(Server.MapPath("~/Uploads"))
            // to split the lines according to commas
          .Select(line => line.Split(','))
          .Select(columns => new { GuestID =ErrorMessage(columns.Length<=8?"":columns[0]), IC_No = ErrorMessage(columns[1]), Grouping = ErrorMessage(columns[2]), Remarks = ErrorMessage(columns[3]), GuestName = ErrorMessage(columns[4]), Class_Group = ErrorMessage(columns[5]), Staff = ErrorMessage(columns[6]), Attendance_Parents_Only = ErrorMessage(columns[7]), Registration = ErrorMessage(columns.Length<=8?"":columns[8]) }); 



        myGridView.DataSource = data; 
        myGridView.DataBind();
       }

我想检查并显示第一列(第0列)中重复项的错误消息。谢谢!

2 个答案:

答案 0 :(得分:0)

 private bool CheckDuplicates(string stringtobeChecked)
{
    foreach (GridViewRow row in GridView_FtpStatus.Rows)
    {
        if ((row.Cells[0].Text) == stringtobeChecked)
            return true;
        else
            return false;
    }
    return false;
}

基于上述结果执行结果操作。希望它有帮助

答案 1 :(得分:0)

这是最好的例子,

For index As Integer = 0 To GridView1.Rows.Count - 1
            If GridView1.Rows(index).Cells(0).Text = "value" Then
                ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "", "<script>alert('duplicate records');</script>", False)
            End If
        Next