private void accView_CellClick(object sender,DataGridViewCellEventArgs e)我有一个带数据库的应用程序。这是一个库存应用程序。当用户向表格添加条目时,他们可以选择上传照片。这不是必需的。一旦他们成功添加了没有照片的新项目,那么应该能够转到主表单并在datagridview上选择行,它将根据所选内容填充一些带有数据的标签。
问题在他们选择没有上传到数据库的照片的行时开始。它抛出一个ArgumentException,指出该路径不是合法的形式。以下是我的代码。我很难解决这个问题。如果选择了没有照片的行,则图片框应该只显示默认照片...
private void accView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
if (!accView.Rows[e.RowIndex].IsNewRow)
{
//Visual Studio shows the error on the line of code below.
accPictureBox.Image = Image.FromFile(accView.Rows[e.RowIndex].Cells[10].Value.ToString(), true);
}
}
更新
private void accView_CellClick(object sender, DataGridViewCellEventArgs e)
{
string defImage = (new System.Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string imageDir = Path.GetDirectoryName(defImage);
string fullPath = Path.Combine(imageDir, @"C:\Users\Brandon\Documents\Visual Studio 2013\Projects\Firearm Asset Management\Firearm Asset Management\bin\Beta\Images\DefaultImage4.jpg");
var path = accView.Rows[e.RowIndex].Cells[10].Value;
//Syntax Error on line below for invalid arguments.
if (string.IsNullOrEmpty(path))
{
//set the default path
path = fullPath;
}
//Syntax Error on line below for invalid arguments.
accPictureBox.Image = Image.FromFile(path, true)
}
答案 0 :(得分:2)
如果没有上传图片,那么可能是
accView.Rows[e.RowIndex].Cells[10].Value
将是null
或空字符串。因此,请检查
string defImage = (new System.Uri(System.Reflection.Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string imageDir = Path.GetDirectoryName(defImage);
string fullPath = Path.Combine(imageDir, @"./Images/DefaultImage4.jpg");
string path = string.Empty;
if (accView.Rows[e.RowIndex].Cells[10].Value != null)
path = accView.Rows[e.RowIndex].Cells[10].Value.ToString();
//Syntax Error on line below for invalid arguments.
if (string.IsNullOrEmpty(path))
{
//set the default path
path = fullPath;
}
//Syntax Error on line below for invalid arguments.
accPictureBox.Image = Image.FromFile(path, true)
答案 1 :(得分:0)
我认为单元格[10]是图像路径的所在。但是,如果用户尚未上传图像,则您将没有路径。
我首先检查单元格[10]的值,如果值是正确形成的路径,那么我将它分配给accPictureBox.Image