0

Scenario: In database we have a column of varchar type that contains the path of images to be displayed and in dotnet application we have a gridview which has columns varchar type. Now in this column we need to display the images which are in application project images folder.

we are using below method but getting System.drawing.bitmap written in gridviewimagecolumn

foreach (DataGridViewRow row1 in dgvDisplayTiles.Rows)
{
    string imgPath;
    imgPath= (Application.StartupPath + row1.Cells[3].Value.ToString());
    Image imageFile = Image.FromFile(imgPath);
    if (imgPath != null)
    {
        //img.Image=imgPath;
        row1.Cells[3].Value = imageFile;
    }
    else
    {
        MessageBox.Show("Invalid Path");
    }
}
2
  • what is the type of your cells[3] cell? Commented Dec 2, 2013 at 10:10
  • @NoviceProgrammer: varchar type Commented Dec 2, 2013 at 10:41

1 Answer 1

1

The problem is that you combine wrong path.

you should use:

imgPath= Path.Combine(Application.StartupPath, row1.Cells[3].Value.ToString());

instead of:

imgPath= (Application.StartupPath + row1.Cells[3].Value.ToString());

full code:

foreach (DataGridViewRow row1 in dgvDisplayTiles.Rows)
{
    string imgPath;
    imgPath= Path.Combine(Application.StartupPath, row1.Cells[3].Value.ToString());
    Image imageFile = Image.FromFile(imgPath);
    if (imgPath != null)
    {
        //img.Image=imgPath;
        row1.Cells[3].Value = imageFile;
    }
    else
    {
        MessageBox.Show("Invalid Path");
    }
}

Update#2 :

You can create dynamic columns like this:

For the easy understanding, i create an Employee class and add some dummy data and bind it with the DataGridView

public class Employee
{
    public string id { get; set; }
    public string name { get; set; }
    public string imagePath { get; set; }
}


private void bindGrid()
{
    List<Employee> employees = new List<Employee>();
    employees.Add(new Employee() {  id = "1",name = "joseph", imagePath = "abc.png" });
    employees.Add(new Employee() {  id = "2", name = "Mac", imagePath = "capture2.png" });
    this.dataGridView1.DataSource = employees;

    DataGridViewImageColumn column = new DataGridViewImageColumn();
    column.Name = "imgColumn1";
    column.HeaderText = "Image";
    this.dataGridView1.Columns.Add(column);
    this.dataGridView1.Refresh();

    foreach (DataGridViewRow row1 in dataGridView1.Rows)
    {
        string imgPath;
        imgPath = Path.Combine(Application.StartupPath, row1.Cells[2].Value.ToString());
        Image imageFile = Image.FromFile(imgPath);

        if (imgPath != null)
        {
            row1.Cells[3].Value = imageFile;
        }
        else
        {
            MessageBox.Show("Invalid Path");
        }
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

it displays same "System.drawing.bitmap" in gridview
what is stored in your row1.Cells[3].Value.ToString() ?
we are assigning image path here. In database all the imagepaths are stored.
in my opinion you can add another column of Image type and set the value of that cell to imageFile, because currently your row1.Cells[3] is of string(varchar) column, while to display image you should have a ImageType column.
requirement was to create the gridview dynamically and fetch the whole database table details into that gridview and we need to display image by reading path in last column i.e. cells[3]. Also can we define column type dynamically in gridview, if yes how to set it of imagetype?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.