Hi, I have a gridview with checkboxes in the 3rd column. I am trying to work out when they are selected. I have the following code:
Code
foreach (GridViewRowInfo dr in gvMovies.Rows)
if (dr.Cells[2].Value = true) MessageBox.Show("Row " + dr.Index + " selected");
I get the following error: Cannotimplicitly convert type 'object' to 'bool'. An explicit conversion exists (are you missing a cast?) I have tried the following but nothing has worked:
Code
foreach (GridViewRowInfo dr in gvMovies.Rows)
if ((bool)dr.Cells[2].Value = true) MessageBox.Show("Row " + dr.Index + " selected");
Code
foreach (GridViewRowInfo dr in gvMovies.Rows)
if (Convert.ToBoolean(dr.Cells[2].Value) = true) MessageBox.Show(dr.Cells[2].Value + " Rows " + dr.Index + " selected");
Thanks for any replies Jay
5 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Sam HobbsRank #107 · 20.5K reputationPosted Jan 30, 2011, 1:38 PM EST
0
Since you are not using controls from .Net, it would help for you to state explicitly what you are using. I don't know how to read the documentation for that control. Unless someone around here is familiar with that control, you need to help us find the relevant documentation. What type of data is the "Value" property? I assume it is an Object, but you probably need to read the control's documentation to determine how to convert the data.
theLizardRank #1,079 · 1.8K reputationPosted Jan 30, 2011, 3:19 PM EST
0
Not knowing the control or how it works I will only guess your solution.
I am assuming that you have assigned a check box to the cell you want to evaluate as Boolean, if so the first thing you need to do is to cast the object in the cell back to a checkbox eg
Code
foreach (GridViewRowInfo dr in gvMovies.Rows)
{ CheckBox cb = dr.Cells[2];
if(cb.Checked) .......
// or maybe this ---- if(((CheckBox cb=dr.Cells[2]).Checked) == true)
}
But I am ONLY guessing at this point.
0
Suthish NairRank #92 · 22.8K reputationPosted Jan 30, 2011, 8:05 PM EST
0
You need to use FindControl method to find the CheckBox control from Row cell.
Jay Webster58 reputationPosted Jan 30, 2011, 9:07 PM EST
0
I found the answer:
foreach (GridViewRowInfo dr in gvMovies.Rows) if (dr.Cells[2].Value == true) MessageBox.Show("Row " + dr.Index + " selected");
I feel silly!
Sorry I forgot to mention I am using "Telerik Winform Controls".
Thanks for all the help!
0
Sam HobbsRank #107 · 20.5K reputationPosted Jan 30, 2011, 9:16 PM EST
0
Yes, you are silly; as silly as every other programmer using either C# or C++; I am not familiar with Java enough to know what it would do. Every programmer of those languages must go through a learning period in which they learn from that mistake. Fortunately C# is less likely to allow you to make that mistake; C++ would accept it as you wrote it and you would get an error during execution, which might be difficult to diagnose.
Sam HobbsRank #107 · 20.5K reputationPosted Jan 30, 2011, 1:38 PM EST
theLizardRank #1,079 · 1.8K reputationPosted Jan 30, 2011, 3:19 PM EST
I am assuming that you have assigned a check box to the cell you want to evaluate as Boolean, if so the first thing you need to do is to cast the object in the cell back to a checkbox eg
CheckBox cb = dr.Cells[2];
if(cb.Checked)
.......
// or maybe this ---- if(((CheckBox cb=dr.Cells[2]).Checked) == true)
}
But I am ONLY guessing at this point.
Suthish NairRank #92 · 22.8K reputationPosted Jan 30, 2011, 8:05 PM EST
Jay Webster58 reputationPosted Jan 30, 2011, 9:07 PM EST
foreach (GridViewRowInfo dr in gvMovies.Rows)
if (dr.Cells[2].Value == true) MessageBox.Show("Row " + dr.Index + " selected");
I feel silly!
Sorry I forgot to mention I am using "Telerik Winform Controls".
Thanks for all the help!
Sam HobbsRank #107 · 20.5K reputationPosted Jan 30, 2011, 9:16 PM EST