Operator '>=' cannot be applied to operands of type 'object' and 'int' in datagridview cell values
I want to make sure that a value of one of my cells in my datagridview is greater than 0 to do something. but it gives me the error I mentioned.
if (dataGridView1.CurrentRow.Cells["totalQTYColumnDataGridViewTextBoxColumn"].Value >= 0) {
do something
}
I dont know how to convert it into Int when I do this it says its wrong:
if (dataGridView1.CurrentRow.Convert.ToInt32(Cells["totalQTYColumnDataGridViewTextBoxColumn"].Value) >= 0) {
do something
}
c# datagridview
add a comment |
I want to make sure that a value of one of my cells in my datagridview is greater than 0 to do something. but it gives me the error I mentioned.
if (dataGridView1.CurrentRow.Cells["totalQTYColumnDataGridViewTextBoxColumn"].Value >= 0) {
do something
}
I dont know how to convert it into Int when I do this it says its wrong:
if (dataGridView1.CurrentRow.Convert.ToInt32(Cells["totalQTYColumnDataGridViewTextBoxColumn"].Value) >= 0) {
do something
}
c# datagridview
2
Possible duplicate of retrieving number value from datagridview C#
– mjwills
Nov 13 '18 at 9:10
You need to convert yourobject
toint
. Otherwise there is no possible comparison. The types of the objects you want to compare are different, thus can't be compared.
– RedFox
Nov 13 '18 at 9:12
add a comment |
I want to make sure that a value of one of my cells in my datagridview is greater than 0 to do something. but it gives me the error I mentioned.
if (dataGridView1.CurrentRow.Cells["totalQTYColumnDataGridViewTextBoxColumn"].Value >= 0) {
do something
}
I dont know how to convert it into Int when I do this it says its wrong:
if (dataGridView1.CurrentRow.Convert.ToInt32(Cells["totalQTYColumnDataGridViewTextBoxColumn"].Value) >= 0) {
do something
}
c# datagridview
I want to make sure that a value of one of my cells in my datagridview is greater than 0 to do something. but it gives me the error I mentioned.
if (dataGridView1.CurrentRow.Cells["totalQTYColumnDataGridViewTextBoxColumn"].Value >= 0) {
do something
}
I dont know how to convert it into Int when I do this it says its wrong:
if (dataGridView1.CurrentRow.Convert.ToInt32(Cells["totalQTYColumnDataGridViewTextBoxColumn"].Value) >= 0) {
do something
}
c# datagridview
c# datagridview
edited Nov 13 '18 at 9:15
Daniel_Ranjbar
asked Nov 13 '18 at 9:09
Daniel_RanjbarDaniel_Ranjbar
828
828
2
Possible duplicate of retrieving number value from datagridview C#
– mjwills
Nov 13 '18 at 9:10
You need to convert yourobject
toint
. Otherwise there is no possible comparison. The types of the objects you want to compare are different, thus can't be compared.
– RedFox
Nov 13 '18 at 9:12
add a comment |
2
Possible duplicate of retrieving number value from datagridview C#
– mjwills
Nov 13 '18 at 9:10
You need to convert yourobject
toint
. Otherwise there is no possible comparison. The types of the objects you want to compare are different, thus can't be compared.
– RedFox
Nov 13 '18 at 9:12
2
2
Possible duplicate of retrieving number value from datagridview C#
– mjwills
Nov 13 '18 at 9:10
Possible duplicate of retrieving number value from datagridview C#
– mjwills
Nov 13 '18 at 9:10
You need to convert your
object
to int
. Otherwise there is no possible comparison. The types of the objects you want to compare are different, thus can't be compared.– RedFox
Nov 13 '18 at 9:12
You need to convert your
object
to int
. Otherwise there is no possible comparison. The types of the objects you want to compare are different, thus can't be compared.– RedFox
Nov 13 '18 at 9:12
add a comment |
1 Answer
1
active
oldest
votes
You have first parse the value as an integer and then make the compare. You can't compare an System.Object
with an integer literal.
var cell = dataGridView1.CurrentRow.Cells["totalQTYColumnDataGridViewTextBoxColumn"];
int totalQTY;
if(int.TryParse(cell.Value?.ToString(), out totalQTY)
&& totalQTY >= 0)
{
}
Note: int.TryParse returns true when parsing succeeds otherwise returns false. When parsing succeeds the parsed value is copied to totalQTY
.
Thank you you solved my problem with details :)
– Daniel_Ranjbar
Nov 13 '18 at 9:25
2
@absolute455 if the value can benull
, I recommend.Value?.ToString()
stackoverflow.com/questions/5646145/…
– Slai
Nov 13 '18 at 9:28
@slai what? you mean I add a question mark? what does it do?
– Daniel_Ranjbar
Nov 13 '18 at 13:15
2
@absolute455 This is called conditional operator. IfValue
isnull
and you callToString
on it you will get aNullReferenceException
and your program would stop unexpectedly to work - unless the corresponding code is wrapped in atry/catch
block. Null conditional operator would save you from that and the call onToString
would be done only whenValue
is not null. For further info please have a look at docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
– Christos
Nov 13 '18 at 13:43
@absolute455 You are very welcome !
– Christos
Nov 14 '18 at 8:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277403%2foperator-cannot-be-applied-to-operands-of-type-object-and-int-in-datagr%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have first parse the value as an integer and then make the compare. You can't compare an System.Object
with an integer literal.
var cell = dataGridView1.CurrentRow.Cells["totalQTYColumnDataGridViewTextBoxColumn"];
int totalQTY;
if(int.TryParse(cell.Value?.ToString(), out totalQTY)
&& totalQTY >= 0)
{
}
Note: int.TryParse returns true when parsing succeeds otherwise returns false. When parsing succeeds the parsed value is copied to totalQTY
.
Thank you you solved my problem with details :)
– Daniel_Ranjbar
Nov 13 '18 at 9:25
2
@absolute455 if the value can benull
, I recommend.Value?.ToString()
stackoverflow.com/questions/5646145/…
– Slai
Nov 13 '18 at 9:28
@slai what? you mean I add a question mark? what does it do?
– Daniel_Ranjbar
Nov 13 '18 at 13:15
2
@absolute455 This is called conditional operator. IfValue
isnull
and you callToString
on it you will get aNullReferenceException
and your program would stop unexpectedly to work - unless the corresponding code is wrapped in atry/catch
block. Null conditional operator would save you from that and the call onToString
would be done only whenValue
is not null. For further info please have a look at docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
– Christos
Nov 13 '18 at 13:43
@absolute455 You are very welcome !
– Christos
Nov 14 '18 at 8:37
add a comment |
You have first parse the value as an integer and then make the compare. You can't compare an System.Object
with an integer literal.
var cell = dataGridView1.CurrentRow.Cells["totalQTYColumnDataGridViewTextBoxColumn"];
int totalQTY;
if(int.TryParse(cell.Value?.ToString(), out totalQTY)
&& totalQTY >= 0)
{
}
Note: int.TryParse returns true when parsing succeeds otherwise returns false. When parsing succeeds the parsed value is copied to totalQTY
.
Thank you you solved my problem with details :)
– Daniel_Ranjbar
Nov 13 '18 at 9:25
2
@absolute455 if the value can benull
, I recommend.Value?.ToString()
stackoverflow.com/questions/5646145/…
– Slai
Nov 13 '18 at 9:28
@slai what? you mean I add a question mark? what does it do?
– Daniel_Ranjbar
Nov 13 '18 at 13:15
2
@absolute455 This is called conditional operator. IfValue
isnull
and you callToString
on it you will get aNullReferenceException
and your program would stop unexpectedly to work - unless the corresponding code is wrapped in atry/catch
block. Null conditional operator would save you from that and the call onToString
would be done only whenValue
is not null. For further info please have a look at docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
– Christos
Nov 13 '18 at 13:43
@absolute455 You are very welcome !
– Christos
Nov 14 '18 at 8:37
add a comment |
You have first parse the value as an integer and then make the compare. You can't compare an System.Object
with an integer literal.
var cell = dataGridView1.CurrentRow.Cells["totalQTYColumnDataGridViewTextBoxColumn"];
int totalQTY;
if(int.TryParse(cell.Value?.ToString(), out totalQTY)
&& totalQTY >= 0)
{
}
Note: int.TryParse returns true when parsing succeeds otherwise returns false. When parsing succeeds the parsed value is copied to totalQTY
.
You have first parse the value as an integer and then make the compare. You can't compare an System.Object
with an integer literal.
var cell = dataGridView1.CurrentRow.Cells["totalQTYColumnDataGridViewTextBoxColumn"];
int totalQTY;
if(int.TryParse(cell.Value?.ToString(), out totalQTY)
&& totalQTY >= 0)
{
}
Note: int.TryParse returns true when parsing succeeds otherwise returns false. When parsing succeeds the parsed value is copied to totalQTY
.
edited Nov 13 '18 at 11:53
answered Nov 13 '18 at 9:12
ChristosChristos
43.7k84476
43.7k84476
Thank you you solved my problem with details :)
– Daniel_Ranjbar
Nov 13 '18 at 9:25
2
@absolute455 if the value can benull
, I recommend.Value?.ToString()
stackoverflow.com/questions/5646145/…
– Slai
Nov 13 '18 at 9:28
@slai what? you mean I add a question mark? what does it do?
– Daniel_Ranjbar
Nov 13 '18 at 13:15
2
@absolute455 This is called conditional operator. IfValue
isnull
and you callToString
on it you will get aNullReferenceException
and your program would stop unexpectedly to work - unless the corresponding code is wrapped in atry/catch
block. Null conditional operator would save you from that and the call onToString
would be done only whenValue
is not null. For further info please have a look at docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
– Christos
Nov 13 '18 at 13:43
@absolute455 You are very welcome !
– Christos
Nov 14 '18 at 8:37
add a comment |
Thank you you solved my problem with details :)
– Daniel_Ranjbar
Nov 13 '18 at 9:25
2
@absolute455 if the value can benull
, I recommend.Value?.ToString()
stackoverflow.com/questions/5646145/…
– Slai
Nov 13 '18 at 9:28
@slai what? you mean I add a question mark? what does it do?
– Daniel_Ranjbar
Nov 13 '18 at 13:15
2
@absolute455 This is called conditional operator. IfValue
isnull
and you callToString
on it you will get aNullReferenceException
and your program would stop unexpectedly to work - unless the corresponding code is wrapped in atry/catch
block. Null conditional operator would save you from that and the call onToString
would be done only whenValue
is not null. For further info please have a look at docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
– Christos
Nov 13 '18 at 13:43
@absolute455 You are very welcome !
– Christos
Nov 14 '18 at 8:37
Thank you you solved my problem with details :)
– Daniel_Ranjbar
Nov 13 '18 at 9:25
Thank you you solved my problem with details :)
– Daniel_Ranjbar
Nov 13 '18 at 9:25
2
2
@absolute455 if the value can be
null
, I recommend .Value?.ToString()
stackoverflow.com/questions/5646145/…– Slai
Nov 13 '18 at 9:28
@absolute455 if the value can be
null
, I recommend .Value?.ToString()
stackoverflow.com/questions/5646145/…– Slai
Nov 13 '18 at 9:28
@slai what? you mean I add a question mark? what does it do?
– Daniel_Ranjbar
Nov 13 '18 at 13:15
@slai what? you mean I add a question mark? what does it do?
– Daniel_Ranjbar
Nov 13 '18 at 13:15
2
2
@absolute455 This is called conditional operator. If
Value
is null
and you call ToString
on it you will get a NullReferenceException
and your program would stop unexpectedly to work - unless the corresponding code is wrapped in a try/catch
block. Null conditional operator would save you from that and the call on ToString
would be done only when Value
is not null. For further info please have a look at docs.microsoft.com/en-us/dotnet/csharp/language-reference/…– Christos
Nov 13 '18 at 13:43
@absolute455 This is called conditional operator. If
Value
is null
and you call ToString
on it you will get a NullReferenceException
and your program would stop unexpectedly to work - unless the corresponding code is wrapped in a try/catch
block. Null conditional operator would save you from that and the call on ToString
would be done only when Value
is not null. For further info please have a look at docs.microsoft.com/en-us/dotnet/csharp/language-reference/…– Christos
Nov 13 '18 at 13:43
@absolute455 You are very welcome !
– Christos
Nov 14 '18 at 8:37
@absolute455 You are very welcome !
– Christos
Nov 14 '18 at 8:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277403%2foperator-cannot-be-applied-to-operands-of-type-object-and-int-in-datagr%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Possible duplicate of retrieving number value from datagridview C#
– mjwills
Nov 13 '18 at 9:10
You need to convert your
object
toint
. Otherwise there is no possible comparison. The types of the objects you want to compare are different, thus can't be compared.– RedFox
Nov 13 '18 at 9:12