gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:00:23 28 4
gpt4 key购买 nike

我有一个上传功能,可以将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)中的重复项。谢谢!

最佳答案

 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;
}

根据以上结果执行结果操作,希望对您有所帮助

关于c# - 在Gridview中检查重复项并显示错误消息(如果存在)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327220/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com