Hi,
I need to check if content of the DataGridView cell is of proper type, but the following code is not working:
DataGridViewRowCollection myCollection = this.dataGridViewMapp.Rows;
DataGridViewRow dr in myCollection
If (dr.Cells[1].Value.GetType() is System.Int16)
{}
Thx for help! | | qrac Wednesday, July 30, 2008 12:13 PM | Well, I think I can't think at something else. And I'm pretty sure there is no already defined method to do what you want. As I've said, the datagridview celltakes an element of type object in itand, unless you explicitly modify it, this is the type it will return to your app: object (not int, not string, etc).
If anyone else has any ideea, I'd also like to hear. | | Lucian Baciu Thursday, July 31, 2008 2:18 PM | | | Lucian Baciu Wednesday, July 30, 2008 1:38 PM |
Unfortunally it is not working there is no method like: DataGridViewCell.ValueType() DataGridViewCell - represents the dataGridView cell object not its content. | | qrac Wednesday, July 30, 2008 1:55 PM | If you try
Code Snippet if (dr.Cells[1].Value.GetType() is typeof(System.Int16))
does this work? | | Lucian Baciu Thursday, July 31, 2008 7:04 AM |
Unfortunately, it is not working as well. typeof() expects to get an object as an argument, not system type. Additionally you can't use typeof() in context with "is", because it is returning anerror. Does any one have any other idea how to solve this problem?
| | qrac Thursday, July 31, 2008 7:46 AM | I've done a little test:
Code Snippet
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add(3);
dataGridView1[0, 0].ValueType = typeof(int);
dataGridView1[0, 0].Value = 2;
if (dataGridView1[0, 0].ValueType == typeof(int))
MessageBox.Show(dataGridView1[0, 0].ValueType.ToString());
}
The type you'll have in the datagridview cell is actually the typeof the datagridview cell. If the type of the cell is in, then you'll have an int hosted in it. If the type is string, it will host a string, and so on. So justverify that the type of the textboxmatches your resired type.
If the type of the cell is not set, that it is object and an Object variable is hosted in it. You can try to cast that object variable to your desired type. Try using int.TryParse(dataGridView1[0, 0].Value.ToString()). If it returns true, than you cansay that you have an integervalue in there.
Hope this post helps more. I do not have Visual Studio here, I had to do the test on other machine. This is why I can't always test my code and for that I appologize.
Regards, | | Lucian Baciu Thursday, July 31, 2008 8:06 AM |
I was trying to use it beafore and it is working but, you can't use it in simpl if condition, because first youhave to declare and out valuewhich is returnedas an output of the Try Parse (), so you have to compare those values plus you need to check if the cell. Value is not null. All that makes code opaque,that's why I was looking forsomething moresimpleand elegant. Thx for your help, mayby you have some more ideas  | | qrac Thursday, July 31, 2008 1:21 PM | Well, I think I can't think at something else. And I'm pretty sure there is no already defined method to do what you want. As I've said, the datagridview celltakes an element of type object in itand, unless you explicitly modify it, this is the type it will return to your app: object (not int, not string, etc).
If anyone else has any ideea, I'd also like to hear. | | Lucian Baciu Thursday, July 31, 2008 2:18 PM |
|