Okay so I feel stupid for asking this and I feel like i am overlooking something, but how do retrieve the value of a datagridview cell as an int. I have used this code to change the column to type int

dataGridView1.Columns[0].CellTemplate.ValueType = typeof(int);

I haven't really had a chance to test this line of code above, but I beleive this will work (this data grid view contains 3 colums for intgredients the first is the count of how much the item is needed, the second column is the type of measurement suc as lbs or oz, and the third column is the name of the ingredient)

So how do I read that first columns value as an int? (I am storing it in an object array as a type int). I mean I could probably do some annoying and wasteful code to convert it from a string to and int but I feel like their is something better to do it.

I mean I know this is how to read a string

store[i].inMeasure(dataGridView1[1, i].ToString());

But how do I read as an int


(oh also if anyone knows a way or a web link to do suggestive typing. By this I mean like excel does where if you start typing a simialr word it gives you an option to have it filled in well for me I want to do it on the second and third column above (think about making a menu with drag and drop and a shopping list is compiled by adding up simialer names))

Recommended Answers

All 5 Replies

Not sure if there is an easier way, but you can use Convert.ToInt32(string) method to convert your string to an integer. Here is a link to the MSDN docs on this method which should provide some insight.

Intead of Convert.ToInt32(string), you can use:

int num = 0;
string strNum = "5";

int.TryParse(strNum, out num);

This way, if for some reason your string is not convertible, it wont throw an error, num will simply remain 0. This will save you a try catch ^^

commented: Exactly right, TryParse is my method of choice for string parsing +3

Intead of Convert.ToInt32(string), you can use:

int num = 0;
string strNum = "5";

int.TryParse(strNum, out num);

This way, if for some reason your string is not convertible, it wont throw an error, num will simply remain 0. This will save you a try catch ^^

Okay so I take it strNum would be the number I want to retrieve and num will be the number as and it the string is coverted to right? is their a way I can wrap an error message into this that says, error in cell what ever this is not a valid #

If this validation step is done, on the cell edit event, you can get the row and column index in the properties of the event arg.

To know if the number has been parsed correctly, you can simple verify the value of num. because the 'out' parameter means that it will send the value to that variable if the parsing sucess.

If this validation step is done, on the cell edit event, you can get the row and column index in the properties of the event arg.

To know if the number has been parsed correctly, you can simple verify the value of num. because the 'out' parameter means that it will send the value to that variable if the parsing sucess.

Actually, thats not entirely true. If the parse fails TryParse returns the default value for the type it was called on; for example, int.TryParse will return the int value or zero.

If you want to know if the parse was successful you can use the return value of the TryParse method.
TryParse "returns" two values; The result of the parse is output to the supplied out parameter whilst the method itself returns a boolean to indicate success or failure.
So you can do something like:

int num = 0;
string strNum = "5";
if(int.TryParse(strNum, out num))
{
    //Parse was successful so num now contains the integer value 5
    //process the parsed number here
}
else
{
    //Parse failed, num contains a default value (for int this is zero)
    //put error handling here
    MessageBox.Show("Please enter a valid number");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.