473,651 Members | 3,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridViewCom boBoxCell and value convertion

Hi,

I've got a Windows Form DataGridView with several
DataGridViewCom boBoxCell columns. The Items collection of the
DataGridViewCom boBoxCell 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
Sep 9 '08 #1
4 2713
How are you obtaining the value?

For example, it works fine below:

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

using (var col = new DataGridViewCom boBoxColumn { DataSource =
values, DataPropertyNam e="Bar"})
using (var grid = new DataGridView {
AutoGenerateCol umns = false, DataSource = foos, Dock =
DockStyle.Fill,
Columns = {col}})
using (var form = new Form {Controls = {grid}}) {
Application.Run (form);
}
}
Sep 9 '08 #2
Hi,

Marc Gravell schrieb:
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
Sep 9 '08 #3
I added (before Application.Run ):

grid.CellEndEdi t += (sender,args) ={
object obj =
grid.Rows[args.RowIndex].Cells[args.ColumnInde x].Value;
form.Text = string.Format(" {0} ({1})", obj, (obj ==
null ? "(null)" : obj.GetType().N ame));
};

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

Marc
Sep 9 '08 #4
Hi,

Marc Gravell schrieb:
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()
{
InitializeCompo nent();
}

protected override void OnLoad(EventArg s e)
{
dataGridViewTes t.Rows.Add();

DataGridViewCom boBoxCell lComboBoxCell =
(DataGridViewCo mboBoxCell) dataGridViewTes t.Rows[0].Cells[0];
lComboBoxCell.D ataSource = new int[] { 1, 2, 3, 4, 5 };
}

private void dataGridViewTes t_CellEndEdit(o bject sender,
DataGridViewCel lEventArgs e)
{
object loValue =
dataGridViewTes t.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
string lstrMsg = string.Format(" Row:{0}, Col:{1}, Type:{2}",
e.RowIndex, e.ColumnIndex, loValue.GetType ());
System.Diagnost ics.Trace.Write Line(lstrMsg);
}
}
}

And the result is:
Row:0, Col:0, Type:System.Str ing
>
Marc
Best regards
Martin
Sep 9 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3022
by: Moshazu | last post by:
I am attempting to use the DataGridView control in my software and have not been able to easily find answers to a couple of questions that I have. 1. Is the only way to change the data in a DataGridViewComboBox during the runtime the way that I have done it here? If not, is there an easier way? Dim tmpCMBRow As New DataGridViewRow tmpCMBRow.CreateCells(Me.grid)
0
3059
by: Jay | last post by:
I am using Windows .Net 2.0 SDK I have a DataGridView control in a windows form. None of my controls are databound prior to run time. I am creating a string array with which I want to populate each DataGridViewComboBoxCell programmatically. Some of the items in the comboboxcell controls may be the same, but most will not, so I can not populate the DataGridViewComboBoxColumn. I have tried adding rows to the datagrid using Rows.Add. I have...
0
5471
by: John | last post by:
Hi, I have set a DataGridViewComboBoxColumn for a complex object type, and set the DisplayMember and ValueMember to members of the object type. My problem is how to programmatically set the cell value. The objects live in the DataGridViewComboBoxColumn.Items, but there is no way of getting an object out based on, e.g. display value. Here's the type :
2
8828
by: kofteros | last post by:
I have a DataGridView in which one of its columns is a DatagridViewCombobox. This column contains values as 1. "ROAMING" 2. "MOBILE" 3. "TELEPHONY"... I want to select an index from this DatagridViewComboboxCell programmatically, for example 2 ("MOBILE"). Is anyone knows an easy way of doing this. Thak you
1
23648
by: hangdee | last post by:
I have a DatagridviewComboBoxCell with self added object (with an id and a string field) as items to the DatagridviewComboBoxCell. Dim cboCell As New DataGridViewComboBoxCell cboCell.Items.Add(new MyObj(1,"Item 1")) cboCell.Items.Add(new MyObj(2,"Item 2")) cboCell.Items.Add(new MyObj(3,"Item 3")) But I cannot find a method from DataGridViewComboBoxCell to get back the selected MyObj in the cboCell list.
1
6441
by: aXqd | last post by:
i want a editable comboboxcell for a datagridview, i have done these: public partial class Form1 : Form { .... private static List<string_tempList = new List<string>(new string {}); ....
0
2142
by: Bruno Neves Pires Silva | last post by:
Hello, everybody. How can I get the selected item index in a DataGridViewComboBoxCell, in a datagridview?
0
3393
by: news.microsoft.com | last post by:
Hello- I'm having a dickens of a time trying to set the value on a DataGridViewComboBoxColumn. I actually have two DataGridViewComboBoxColumns in my datagrid view. First, I'm setting up the grid/column as follows.. DataGridViewComboBoxColumn someColumn = new DataGridViewComboBoxColumn();
0
2860
by: =?Utf-8?B?R2FkeUM=?= | last post by:
HI I populate one col. in a dataiewgrid with a combocell as DataGridViewComboBoxCell. (create on the fly and assign it to cell) I need to avid arrow user's up/down on that cells. All the grid events ignore edit state of combocell. Any idea? Thanks in advance.
0
8277
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8803
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8581
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.