![]() But I'm not super familiar with it, because I've been blessed for years with having no need to use a DataGridView. If you're really clever, I seem to remember a way to make a customized DataTable that returns customized DataRows that have properties instead of requiring cell indexing. If, instead of directly using the DGV, you set up a DataTable and configure its columns, the rows you get back from that DataTable will let you get their contents in a way that doesn't require so much conversion. You're right, that's part of why I hate the DataGridView. Now, you might be thinking it's a pain in the butt to keep track of this. When I convert them to what I want, I store the result in a name without "raw" so I know that one's safe. It helps me know when I see it that this thing is dangerous, and may not be what I want. I like to use the word "raw" in variable names that represent Objects that need casting or Strings that need parsing. If you're ever debugging an error, you will praise the day you chose to use more lines than less lines. This won't work:Ĭode: Dim rawCellValue As Object = DataGridView1.Rows(1).Cells(0).Valueĭim cellValue As Integer = CInt(rawCellValue)Some people hiss and spit that this uses two whole lines. ![]() Now, suppose later, you know a cell has an Integer value in it, and you want to get it. When you see that error message, you should go look at the documentation for everything on that line and figure out where the type mismatch is. Your hint was an error message that probably said "Cannot convert value of type 'Object' to type 'String'", or "No overload of MessageBox.Show can be called with these arguments". ![]() How useful that thing is varies greatly by type.Īnyway, the reason your first code sample(s) failed (I seem to remember another line being there) is MessageBox.Show() wants a String, but you were giving it an Object. So if you have an Object variable and call its ToString(), you'll always get something. ToString() seems like magic, and it is handy. Now, that's sort of what's happening here. And for more complicated things, you need CType() or DirectCast(). There's CStr() for Strings, though ToString() works. Once you put an Integer in an Object variable, you aren't getting an Integer back out of it without using a cast like CInt(). There's really no other type Value could be, given that it is forced to represent so much.īut Object can't be automatically converted to anything, since it could be anything. You can put a String in an Object variable. You can put an Integer in an Object variable. Object is the choice because it can "hold" anything. That's one of the symptoms of DataGridView: it has to work with lots of data types, so if you ask it for a value you always get Object. The end result is you use the DataGridViewCell.Value property. In this case, what your line is doing is pretty complex. ![]() This wasn't so true in VBA or VB6, it was plenty happy to convert back and forth between them. (Personally I hate how DataGridView displays data and I think unless you go way out of your way to use typed datasets, it teaches you to write very sloppy code, this is part of why.) It will help if you understand what's going on here, I think a lot of people encounter this and decide that ToString() is some magic function that makes VB start working.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |