我有這些代碼上傳CSV文件。另外,我已經放置了一個GridView來顯示它的內容。現在,我想要做的就是顯示來自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。如果是這樣,任何人都可以顯示我的代碼來做到這一點?
您上傳CSV文件的事實是不相關的問題。你的問題是如何在GridView中的單元格中指示錯誤。 –
@約翰桑德斯,哦!對不起,我錯誤地格式化了這個問題,將其改爲 – Mark20