Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从datagridview行填充picturebox_C#_Datagridview_Picturebox - Fatal编程技术网

C# 从datagridview行填充picturebox

C# 从datagridview行填充picturebox,c#,datagridview,picturebox,C#,Datagridview,Picturebox,私有void accView_CellClick(对象发送者,DataGridViewCellEventArgs e)我有一个带有数据库的应用程序。这是一个库存应用程序。当用户向表中添加条目时,他们可以选择上载照片。这不是必需的。一旦他们在没有照片的情况下成功添加了新项目,那么他们应该能够转到主窗体并选择datagridview上的行,并且它将根据所选内容使用数据填充一些标签 当他们选择没有上传到数据库的照片的行时,问题就开始了。它抛出ArgumentException,指出路径不是合法形式。下

私有void accView_CellClick(对象发送者,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)
    }

如果没有上传图像,那么

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)

我想单元格[10]是图像路径应该在的地方。但是,如果用户没有上传图像,您将没有路径


我会首先检查单元格[10]的值,如果该值是正确形成的路径,那么我会将其分配给accPictureBox.Image

谢谢barrick。我有代码,但是如果我要通过安装程序将此部署到另一台机器上,我将如何制定默认映像的路径?部署中是否包含默认映像?如果是这样的话,你可以确定它的位置,应该可以帮助你找到图像文件的正确路径。谢谢,我会试试这个。如果我有任何问题,我会让你知道。巴里克,我更新了OP以显示一些新代码,我还没有放置绝对路径(当我下班回家时)。我不是100%确定如何为我的问题编码。我确信我上面的语法有问题……这对我来说很好,您只需要在
if
语句中设置
path=fullPath