Datagrid Shows Path of Image Instead of Image Itself

DataGrid shows path of image instead of image itself

first of all, DataGrid generate DataGridTextColumns by default and we have to use AutoGeneratingColumn event to change type of column. We need to use DataGridTemplateColumn which contains Image in template (image source should be bound to correct DataTable column). The best place to define template is in Resources.

here is how the issue can be solved:

xaml part

<DataGrid Name="ChannelDataGrid" AutoGeneratingColumn="ChannelDataGrid_OnAutoGeneratingColumn">

<DataGrid.Resources>
<DataTemplate x:Key="ImgCell">
<Image Source="{Binding Path=Img}"/>
</DataTemplate>
</DataGrid.Resources>
</DataGrid>

code:

private void InitializeDataTable()
{
System.Data.DataTable DataTable = new System.Data.DataTable
{
Columns = {"Test #", "Img", "Min Range", "Max Range", "Result"}
};

Uri uri = new Uri(@"C:/Users/User/Desktop/szagdoga/error.png");

for (int i = 6; i < 50; i++)
DataTable.Rows.Add(ExcelFile[0, i], uri, ExcelFile[1, i], 0, 0);

ChannelDataGrid.ItemsSource = DataTable.DefaultView;
}

private void ChannelDataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "Img")
{
// replace text column with image column
e.Column = new DataGridTemplateColumn
{
// searching for predefined tenplate in Resources
CellTemplate = (sender as DataGrid).Resources["ImgCell"] as DataTemplate,
HeaderTemplate = e.Column.HeaderTemplate,
Header = e.Column.Header
};
}
}

Show Image in dynamically generated DataGrid.Columns in WPF

You could handle the AutoGeneratingColumn event and programmatically create a DataGridTemplateColumn that contains an Image element. Try this:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName != "Identifiant" && e.PropertyName != "=>")
{
FrameworkElementFactory image = new FrameworkElementFactory(typeof(Image));
image.SetBinding(Image.SourceProperty, new Binding(e.PropertyName));

e.Column = new DataGridTemplateColumn
{
CellTemplate = new DataTemplate() { VisualTree = image },
Header = e.PropertyName
};
}
}

Error loading image in the DataGridView in C#

These two lines make no sense:

DataGridViewImageColumn Image = new DataGridViewImageColumn();
Image = (DataGridViewImageColumn)dataGridViewStudent.Columns[7];

If your columns are being auto-generated the image column will be generated as such and work without further code. This is the simplest way.

And since you don't create any other columns that seems to be what you are doing. So what shall those lines achieve??

Especially as you don't add the extra column anywhere? (Which btw you can't do anyway, as it is already part of the columns collection..)

If you create your dgv columns in code you will have to give each the correct type and connect each one to the right datasource column and add them to the DGV:

    string sql = "Select preview, text from Covers;";
..
..
DataSet ds = new DataSet();
adapter.Fill(ds);

// either simply set this to true or..
dataGridView1.AutoGenerateColumns = false;
// add all columns..
DataGridViewImageColumn dvgic = new DataGridViewImageColumn();
// setting their names
dvgic.Name = "preview";
dvgic.HeaderText = "Preview";
// and relating them to the data source fields..
dvgic.DataPropertyName = "preview";
dataGridView1.Columns.Add(dvgic);

dataGridView1.Columns.Add("text", "Text");
dataGridView1.Columns["text"].DataPropertyName = "text";

dataGridView1.DataSource = ds.Tables[0];

If for some reason you need to set an ImageColumn cell to an Image, be it from whatever source, including a db field that is feeding into a memorystream, then you will have to set the Value of the cell, not the cell itself.

But your error message seems to indicate that at least some of your data are not valid images..

C# WinForms & SQL Server : how to render image in gridview using stored procedure

You will need to add new column for the image and make the image path visibility = false

so your code will be like that :-

sqlCon = new SqlConnection(connectionString);
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();

SqlDataAdapter sqlDA = new SqlDataAdapter("[sp_selectTop10CycleTime]", sqlCon);
sqlDA.SelectCommand.CommandType = CommandType.StoredProcedure;

sqlDA.SelectCommand.Parameters.AddWithValue("@date", DateTime.Now.ToShortDateString());

DataTable dataTbl = new DataTable();

sqlDA.Fill(dataTbl);

dgvNextCycleTime.DataSource = dataTbl;

sqlCon.Close();
//////////////////////////////// New code ////////////////////////////
int ImagePathIndex = 0; // assuming that the image path column is 0 (See in your case)
dgvNextCycleTime.Columns[ImagePathIndex].Visible = false; // Make visibility for the path = false

DataGridViewImageColumn ImageColunm = new DataGridViewImageColumn();
ImageColunm.HeaderText = "Image";
ImageColunm.Name = "ImageName";
ImageColunm.ImageLayout = DataGridViewImageCellLayout.Stretch;
ImageColunm.Image = null;
dgvNextCycleTime.Columns.Add(ImageColunm); // Add the new colunm to the grid

// Add the image to the new column in the grid
foreach (DataGridViewRow row in dgvNextCycleTime.Rows)
{

Image img = Image.FromFile(row.Cells[ImagePathIndex].Value.ToString());
DataGridViewImageCell cell = row.Cells["ImageName"] as DataGridViewImageCell;
cell.Value = img;
}


Related Topics



Leave a reply



Submit