473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to convert an object to a Control

Hi all,

i am trying to convert a dataGridView cell to a control

i used the codes
Expand|Select|Wrap|Line Numbers
  1. Control dat = new Control();
  2. dat = Convert.ChangeType(dataGridView1[colIndex,rowIndex],typeof(Control));
  3.  

I am fetching the values of colIndex and rowIndes from a index code... the problem is even though i tried many codes to convert, it does not work

The expection i am gettting is
"Can not implicitly convert an object to a control. An explicit convertion exist(are u missing a cast?)"

Any help
thanks
vince
Nov 21 '11 #1
3 11148
GaryTexmo
1,501 Expert 1GB
The ChangeType method returns an object, so you need a cast.

Expand|Select|Wrap|Line Numbers
  1. Control dat = null;
  2. dat = (Control)Convert.ChangeType(dataGridView1[colIndex,rowIndex],typeof(Control));
In this case, it's entirely possible that the row/column index of your DataGridView doesn't contain a control. In this case I'd recommend you use the "as" keyword. This will attempt the cast and if unsuccessful, assign null to the object. This will allow you to do a simple check. For example...

Expand|Select|Wrap|Line Numbers
  1. Control dat = null;
  2.  
  3. dat = Convert.ChangeType(dataGridView1[colIndex,rowIndex],typeof(Control)) as Control;
  4. if (dat == null)
  5. {
  6.   throw new Exception(string.Format("Expected control object not found in column {0} and row {1}.", colIndex, rowIndex));
  7. }
  8.  
  9. // If execution reaches this point, dat holds a reference to your control.
Nov 21 '11 #2
Thanks Mr.Gary .. but it is showing "invalid cast expection must implement IConvertible"... i dont know what to do...
Nov 21 '11 #3
GaryTexmo
1,501 Expert 1GB
Ah, the thing about Convert.ChangeType is that it needs to implement that interface, IConvertible. It's implemented for most base types, but apparently not for a Control. Which is fine... just don't use that. If the object in that row/column index is already a control, you can just cast it.

Expand|Select|Wrap|Line Numbers
  1. Control dat = dataGridView1[colIndex, rowIndex] as Control;
  2. ...
I typically use Convert.ChangeType when I'm using reflection to do casts where the type isn't known at compile time. In this particular case, you know you want to cast it to a Control so you can just go ahead and do that.
Nov 21 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Max | last post by:
I just wonder if there is a way of converting an object to a string. I am using a function to send an object parameter e.g "fctng(train0);" later on I would like to use train0 but as a string to...
2
by: seth | last post by:
The Environment: I have a 3rd party control referenced as an object on an aspx page with inline code in script tags accessing the methods of properties of same. I also have a Codebehind page...
2
by: Nikolay Petrov | last post by:
How can I convert variable of type Object to Byte array? TIA
5
by: tienlx | last post by:
Hi all, I'm have a problem that i don't know how to convert object to char in C# . Does anybody know ? Thanks.
2
by: Kimelia Schiles | last post by:
Hi, I would like to know how to convert the value with return type of object to a byte array. For example: byte photo = ds.Tables.Rows; Thanks and have a nice day
0
by: bacsumu | last post by:
i'm programming in c# i use embeded webbrowser ----------------------------------------------------- int Exec( ref Guid pguidCmdGroup, int nCmdID, int nCmdexecopt,
2
by: rajasekar.karthik | last post by:
HI Is there any method to convert calendar control to image in ASP.net since i need to generate a report in the gif format. with the current date highlighted . Regards ...
0
by: suneethadotnet | last post by:
hi , object objee= obj1.GetSecurityInfo("hi"); here GetSecurityInfo("hi") returns struct.i am assigning it to object.now the object objee have all the properties of GetSecurityInfo("hi")...
5
by: deepthisoft | last post by:
hi, I have converted vector to object. How can I convert Object into Vector. My code look like this, Vector column=new Vector(); column.add("one");
2
by: Charming12 | last post by:
Hi All, I am stuck with a unique problem, I have a class whose object i need to convert to a byte to send it further. I am using microsoft's Binaryformatter to convert object to byte as per...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.