Attachment: file.
I am writting some code to show images. This code is written in Form_Load event.
But I am unable to see images. If form is shown without MDI parent, it works fine. But when I set MDI Parent then it doesnt show images.
I moved the code to activate event of form But it shows images after 2/3 seconds.
Could anybody elaborate solution/reason?

Dani AI

Generated

is right that assigning an Image into every cell is expensive — but the symptom you describe () — images only appearing when the child is activated or several seconds later — is usually a timing/paint issue rather than a DataTable bug. Form.Load runs before the child has been shown and fully painted when it’s an MDI child, so heavy UI work there can be deferred by the WinForms message loop and only complete after the form is visible (that’s why moving things to Activate made them appear later).

Practical fixes (pick one that fits your design):

  • Defer the assignment until the form is shown (Shown/OnShown) or queue it with BeginInvoke so the UI finishes initial layout first. Example:

    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        LoadImagesIntoGrid(); // do binding or cell assignments here
    }
  • Don’t store actual Image objects in the DataTable. Keep a small key/ID in the table and supply images on demand from an in-memory cache via an unbound DataGridViewImageColumn. Use CellFormatting (or VirtualMode/CellValueNeeded) to map key → Image at render time; this keeps your DataTable light and avoids duplicating bitmaps.

    private void dataGridView1_CellFormatting(object s, DataGridViewCellFormattingEventArgs e)
    {
        if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewImageColumn)
        {
            var key = dataGridView1.Rows[e.RowIndex].Cells["ImageKey"].Value as string;
            e.Value = imageCache.TryGetValue(key, out var img) ? img : null;
        }
    }

Extra troubleshooting tips: force a redraw after loading with dataGridView1.Invalidate() / dataGridView1.Update(). If you have flicker or slow painting, enable double buffering on the DataGridView via the non-public DoubleBuffered property (reflection). If image decoding is slow, load/scale images on a background thread and marshal only the final Image to the UI thread with BeginInvoke. Avoid Application.DoEvents(); use proper deferral or background work instead.

Summary: prefer deferring the UI work to Shown/BeginInvoke, or use keys + on-demand image provisioning (CellFormatting/VirtualMode) so you don’t assign an Image object into every cell. This addresses both the MDI timing issue and the performance concerns raised by .

Recommended Answers

All 2 Replies

Don't assign a value to each cell.

private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            for (int i = 1; i <= 100; i++){
                dt.Columns.Add("i"+i, typeof(Image));
            }
            for (int i = 1; i <= 100; i++){
                DataRow r = dt.NewRow();
                for (int j = 0; j < 100; j++){
                    r[j] = imageList1.Images[1];
                }
                dt.Rows.Add(r);
            }
            dataGridView1.DataSource = dt;
        }

Thanks for instant reply. But due to some reasons, I am unable to store images in datatable. There are multiple functionalities on grid like drag drop, move, copy etc. Is there any other way to get rid of this problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.