Click here to Skip to main content
14,937,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all i am new on this form.
I am a hobby programmer.
I have program with a datagridview an a datatable.
I have in a folder txt file whit 5 items example 123;in a closet
456;in a drawer
789;in the cellar
and so on.
I have also a folder whit 5 images example 123.png
456.png
789.png
and so on.
I load the txt file in a datatable whit split string column 0 = 123 column 1 = in a closet
second row have column 0 = 456 column 1 = in a drawer
and so on.

now i wil load in the image column the image wich has the same number as the number in column 0;
i do get this with the txt file but how load the image to the richt imagecolumn.
I have a button on the form {ADD ALL} when i press this button then i would like my whole txt file data and the images whit the same numbers in the datagridview showing.
Now the datagridview shows the whole txt data write but in the datagrid image columns
it shows for every row the same image.
In this example i only have 5 items in the txt file and in the image folder.
But my txt file and image folder that i want to use contains more then 1200 items.
thanks for helping my
regards
Patrick

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using PrintDataGrid;
using System.Data.Common;

namespace Pats_Test_Printer
{
public partial class Form1 : Form
{
private List<sp_keramlijst> _SpNummer;
public Form1()
{
InitializeComponent();
}

DataTable dt = new DataTable();

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
dataGridView1.Columns[0].Name = "id";
this.dataGridView1.Rows[e.RowIndex].Cells["id"].Value = (e.RowIndex + 1).ToString();
}

private void Form1_Load(object sender, EventArgs e)
{

textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;


LoadSpNummer(Application.StartupPath + "\\SP.txt");

dt.Columns.Add("NUMMER", typeof(string));
dt.Columns.Add("LOCATIE", typeof(string));
dt.Columns.Add("FOTO", typeof(Image));

dt.Columns.Add("AANTAL ITEMS", typeof(int));
dt.Columns["AANTAL ITEMS"].AutoIncrement = true;
dt.Columns["AANTAL ITEMS"].AutoIncrementSeed = 1;
dt.Columns["AANTAL ITEMS"].AutoIncrementStep = 1;

dataGridView2.Visible = false;

DataGridViewImageColumn SP_Image = new DataGridViewImageColumn();
SP_Image.HeaderText = "FOTO";
SP_Image.ImageLayout = DataGridViewImageCellLayout.Zoom;
SP_Image.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
id.Width = 28;
id.HeaderText = "ID";

DataGridViewTextBoxColumn nummer = new DataGridViewTextBoxColumn();
nummer.HeaderText = "NUMMER";

DataGridViewTextBoxColumn locatie = new DataGridViewTextBoxColumn();
locatie.HeaderText = "LOCATIE";

dataGridView1.Columns.Add(id);
dataGridView1.Columns.Add(nummer);
dataGridView1.Columns.Add(locatie);
dataGridView1.Columns.Add(SP_Image);

dataGridView1.DefaultCellStyle.Font = new Font("tahoma", 10, FontStyle.Bold);
dataGridView1.Columns[0].DefaultCellStyle.Font = new Font("tahoma", 10, FontStyle.Bold);
dataGridView1.Columns[1].DefaultCellStyle.Font = new Font("tahoma", 10, FontStyle.Bold);
dataGridView1.Columns[2].DefaultCellStyle.Font = new Font("tahoma", 10, FontStyle.Bold);
dataGridView1.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView1.RowTemplate.Height = 110;
dataGridView1.AllowUserToAddRows = false;

dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView2.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView2.RowTemplate.Height = 110;

}

private void Bt_Search_Click(object sender, EventArgs e)
{
bool is_found = false;

string SpNummer_Zoeken = textBox1.Text;

try
{
foreach (Sp_Keramlijst Gereedschap_Nummer in _SpNummer)
{

if (SpNummer_Zoeken == Gereedschap_Nummer.SpNummer)
{

is_found = true;

LaadFoto(SpNummer_Zoeken);

textBox2.Text = Gereedschap_Nummer.SpNummerLocatie;

}
}

if (is_found == false)
{
ShowMessageInTextBox("TOOLBOX EMPTY");
}
}
catch (Exception ex)
{
ShowMessageInTextBox(ex.Message);
}
}

private void LoadSpNummer(string file_name)
{
try
{
AutoCompleteStringCollection autocomplete_string_collection = new AutoCompleteStringCollection();

_SpNummer = new List<sp_keramlijst>();

var lines = File.ReadLines(file_name);

foreach (string line in lines)
{
string[] split_line = line.Split(';');
Sp_Keramlijst temp_item = new Sp_Keramlijst(split_line[0], split_line[1]);

autocomplete_string_collection.Add(temp_item.SpNummer);

_SpNummer.Add(temp_item);
}

textBox1.AutoCompleteCustomSource = autocomplete_string_collection;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void LaadFoto(string Foto_naam)
{
string full_path = "";

DirectoryInfo directory_info = new DirectoryInfo(Application.StartupPath + "\\images");
FileInfo[] image_files = directory_info.GetFiles();

try
{
if (image_files.Length > 0)
{
foreach (FileInfo image_file in image_files)
{
bool image_found = image_file.FullName.Contains(Foto_naam);
if (image_found == true)
{
full_path = image_file.FullName;
break;
}
}
}


if (string.IsNullOrEmpty(full_path) == false)
{

Image item_image = Image.FromFile(full_path);

pictureBox1.Image = item_image;
}
else
{
pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\NoImageAvailable.jpg");
}
}
catch (Exception ex)
{
ShowMessageInTextBox(ex.Message);
}
}

private void ShowMessageInTextBox(string messages)
{
MessageBox.Show(messages);
}

/// Text box key Down event
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
// If the key pressed matches the Enter key
if (e.KeyCode == Keys.Enter)
{
// Trigger the button search click event
Bt_Search_Click(this, new EventArgs());
}
}

private void AddToList_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();
Bitmap img1 = new Bitmap(pictureBox1.Image, new Size(143, 107));

int count = dataGridView1.RowCount;
count.ToString();
dataGridView1.Rows.Add(count, textBox1.Text, textBox2.Text, img1);

}

private void Bt_Print_Click(object sender, EventArgs e)
{
// Calling DataGridView Printing
PrintDGV.Print_DataGridView(dataGridView2);
}

private void RmRow_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
}

private void ClList_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
dt.Clear();
dataGridView2.Visible = false;
dataGridView1.Visible = true;


}

private void AddAll_Click(object sender, EventArgs e)
{

dataGridView1.Visible = false;
dataGridView2.Visible = true;

System.IO.StreamReader file = new System.IO.StreamReader(Application.StartupPath + "\\SP.txt");

string newline;
while ((newline = file.ReadLine()) != null)
{
string[] Sp_Keram_line = newline.Split(';');

MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();
Bitmap img1 = new Bitmap(pictureBox1.Image, new Size(143, 107));

dt.Rows.Add(Sp_Keram_line[0], Sp_Keram_line[1], img1);

}
file.Close();

dataGridView1.Rows.Clear();
textBox1.Clear();
textBox2.Clear();
dataGridView2.Visible = true;
dataGridView2.DataSource = dt;
pictureBox1.Image = null;
}
}
}
Posted
Updated 27-Feb-21 22:11pm
v2
Comments
Gerry Schmitz 27-Feb-21 12:30pm
   
Need more "code".

https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-work-with-image-columns-in-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8
Member 14270871 28-Feb-21 4:12am
   
Hello Gerry
I add now my hole code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900