Hi all,
i am trying to convert a dataGridView cell to a control
i used the codes -
Control dat = new Control();
-
dat = Convert.ChangeType(dataGridView1[colIndex,rowIndex],typeof(Control));
-
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
3 11148
The ChangeType method returns an object, so you need a cast. - Control dat = null;
-
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... - Control dat = null;
-
-
dat = Convert.ChangeType(dataGridView1[colIndex,rowIndex],typeof(Control)) as Control;
-
if (dat == null)
-
{
-
throw new Exception(string.Format("Expected control object not found in column {0} and row {1}.", colIndex, rowIndex));
-
}
-
-
// If execution reaches this point, dat holds a reference to your control.
Thanks Mr.Gary .. but it is showing "invalid cast expection must implement IConvertible"... i dont know what to do...
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. - Control dat = dataGridView1[colIndex, rowIndex] as Control;
-
...
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: Nikolay Petrov |
last post by:
How can I convert variable of type Object to Byte array?
TIA
|
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.
|
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
|
by: bacsumu |
last post by:
i'm programming in c#
i use embeded webbrowser
-----------------------------------------------------
int Exec(
ref Guid pguidCmdGroup,
int nCmdID,
int nCmdexecopt,
|
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
...
|
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")...
|
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");
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |