DataGridViewComboBoxCell and value convertion

M

Martin Horst

Hi,

I've got a Windows Form DataGridView with several
DataGridViewComboBoxCell columns. The Items collection of the
DataGridViewComboBoxCell can take any kind of objects. But when the user
selects another item inside the combobox, the Value property always
returns a string. In my case, I need the real object which was added to
the items collections. Is there any way to prevent this conversion.

Thanks in advance
Martin
 
M

Marc Gravell

How are you obtaining the value?

For example, it works fine below:

class Foo {
public int Bar { get; set; }
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
int[] values = new [] { 1,2,3,4,5};
Foo[] foos = new[] { new Foo { Bar = 4 }, new Foo { Bar =
2 } };

using (var col = new DataGridViewComboBoxColumn { DataSource =
values, DataPropertyName="Bar"})
using (var grid = new DataGridView {
AutoGenerateColumns = false, DataSource = foos, Dock =
DockStyle.Fill,
Columns = {col}})
using (var form = new Form {Controls = {grid}}) {
Application.Run(form);
}
}
 
M

Martin Horst

Hi,

Marc said:
How are you obtaining the value?

in the moment I use the Value property. I tried your suggestion to use
the DataSource Property but with the same result. After I selected an
item inside a combobox and leave the cell, in the CellEndEdit event
handler the value of the Value property is always a string.

Thanks and best regards
Martin
 
M

Marc Gravell

I added (before Application.Run):

grid.CellEndEdit += (sender,args) => {
object obj =
grid.Rows[args.RowIndex].Cells[args.ColumnIndex].Value;
form.Text = string.Format("{0} ({1})", obj, (obj ==
null ? "(null)" : obj.GetType().Name));
};

and it is reporting Int32 (not String); can you reproduce this
behaviour in a short (but complete) example?

Marc
 
M

Martin Horst

Hi,

Marc said:
I added (before Application.Run):

I can't do that because it is an MDI application and my MDI windows may
be opened or not.
...


and it is reporting Int32 (not String); can you reproduce this
behaviour in a short (but complete) example?

Yes, here ist a short example:

namespace TestApp2008
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
dataGridViewTest.Rows.Add();

DataGridViewComboBoxCell lComboBoxCell =
(DataGridViewComboBoxCell) dataGridViewTest.Rows[0].Cells[0];
lComboBoxCell.DataSource = new int[] { 1, 2, 3, 4, 5 };
}

private void dataGridViewTest_CellEndEdit(object sender,
DataGridViewCellEventArgs e)
{
object loValue =
dataGridViewTest.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
string lstrMsg = string.Format("Row:{0}, Col:{1}, Type:{2}",
e.RowIndex, e.ColumnIndex, loValue.GetType());
System.Diagnostics.Trace.WriteLine(lstrMsg);
}
}
}

And the result is:
Row:0, Col:0, Type:System.String

Best regards
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top