Jef Patat

Hi all,

I just started a very simple tool which should create some resource files for me. For the moment it is just a form with a datagridview and only this code:

private void Form1_Load(object sender, EventArgs e)
{
DataSet bitmapSet = new DataSet();
bitmapSet.ReadXml("bitmaps.xml");
dataGridView.DataSource = bitmapSet;
dataGridView.DataMember = "bitmap";
DataGridViewImageColumn bitmapColumn = new DataGridViewImageColumn();
bitmapColumn.Name = "bitmapColumn";
bitmapColumn.HeaderText = "Bitmap";
dataGridView.Columns.Add(bitmapColumn);
dataGridView.Columns.Add("widthColumn", "Width");
dataGridView.Columns.Add("heightColumn", "Height");
for (int i = 0; i < dataGridView.Rows.Count-1; i++)
{
dataGridView.RowsIdea.Cells["bitmapColumn"].Value = Image.FromFile(dataGridView.RowsIdea.Cells["Filename"].Value.ToString());
dataGridView.RowsIdea.Cells["widthColumn"].Value = Image.FromFile(dataGridView.RowsIdea.Cells["Filename"].Value.ToString()).Width;
dataGridView.RowsIdea.Cells["heightColumn"].Value = Image.FromFile(dataGridView.RowsIdea.Cells["Filename"].Value.ToString()).Height;
}
}

Everything works fine, but when I reorder/sort a column the dynamically added data disappears. How can I prevent this from happening I do not wish to save this data in the xml if I save the dataset later on.



Re: Windows Forms Data Controls and Databinding Datagridview dynamically added cell contents removed after sorting

SaadH

Hi,

I am having exactly the same problem. Can't find any literature regarding this problem. Is it a VS2005 bug, or am I missing something





Re: Windows Forms Data Controls and Databinding Datagridview dynamically added cell contents removed after sorting

Gavin Jin - MSFT

Why not insert your date to the dataset if you don't want to reflect with xml, you can drop them right before you want to save.





Re: Windows Forms Data Controls and Databinding Datagridview dynamically added cell contents removed after sorting

Zhi-Xin Ye - MSFT

Do the assignment work in cellFormatting event, then it won't disappear when sorting.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Samples

{

public partial class DgvImageCol : Form

{

public DgvImageCol()

{

InitializeComponent();

}

private void DgvImageCol_Load(object sender, EventArgs e)

{

DataSet bitmapSet = new DataSet();

bitmapSet.ReadXml("bitmaps.xml");

dataGridView1.DataSource = bitmapSet;

dataGridView1.DataMember = "bitmap";

DataGridViewImageColumn bitmapColumn = new DataGridViewImageColumn();

bitmapColumn.Name = "bitmapColumn";

bitmapColumn.HeaderText = "Bitmap";

dataGridView1.Columns.Add(bitmapColumn);

dataGridView1.Columns.Add("widthColumn", "Width");

dataGridView1.Columns.Add("heightColumn", "Height");

}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

if (e.RowIndex >= 0 && e.RowIndex < this.dataGridView1.Rows.Count-1)

{

Image img = Image.FromFile(this.dataGridView1.Rows[e.RowIndex].Cells["Filename"].Value.ToString());

if (e.ColumnIndex == 1)//the image column's index

{

e.Value = img;

}

if (e.ColumnIndex == 2)

{

e.Value = img.Width;

}

if (e.ColumnIndex == 3)

{

e.Value = img.Height;

}

}

}

}

}

Hope it helps. Regards,.

Ye






Re: Windows Forms Data Controls and Databinding Datagridview dynamically added cell contents removed after sorting

hrubesh

try this:

private void Form1_Load(object sender, EventArgs e)

{

dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);

}

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

{

if (e.ColumnIndex == 9)

{

e.Value = Image.FromFile(dataGridView1["Filename" ,e.RowIndex ].Value.ToString());

}

}