Here is a string column in my GridView:
Convert.ToString(GridView1.Rows[t].Cells[2].Text)
I tried to convert it to double column:
Convert.ToDouble(GridView1.Rows[t].Cells[2].Text)
also tried this:
double x = double.Parse(GridView1.Rows[t].Cells[2].Text)
but always throws exception: "Input string was not in a correct format"
the code only runs if I type this way Convert.ToString(GridView1.Rows[t].Cells[2].Text)
but there are number values in it and need to get values as a number not a string…
I am using bound field and template field in GridView:
<asp:GridView ID="GridView1" runat="server" Width ="219px" Height="258px">
<Columns>
<asp:BoundField AccessibleHeaderText="k1" HeaderText="k1" />
<asp:BoundField AccessibleHeaderText="k2" HeaderText="k2" />
<asp:TemplateField AccessibleHeaderText="Rank1" HeaderText="Rank1">
<ItemTemplate>
<asp:Label ID="lblRank" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is the Rank Button code:
protected void Rank_Button_Click(object sender, EventArgs e)
{
SortedDictionary<double, int> MyRank = new SortedDictionary<double, int>();
foreach (GridViewRow gRow in GridView1.Rows)
{
if (Convert.ToDouble(gRow.Cells[0].Text) != 0)
{
double score = Convert.ToDouble(gRow.Cells[0].Text);
while (MyRank.ContainsKey(score))
score += 0.00001;
MyRank.Add(score, gRow.RowIndex);
//MyRank.Add(Convert.ToDouble(gRow.Cells[0].Text), gRow.RowIndex);
gRow.Cells[0].ForeColor = System.Drawing.Color.DarkBlue;
}
}
for (int irS = 0; irS < GridView1.Rows.Count; irS++)
{
if (Convert.ToDouble(GridView1.Rows[irS].Cells[0].Text) > 0)
{
ir = ir + 1;
}
}
foreach (KeyValuePair<double, int> OneRank in MyRank)
{
GridViewRow gRow = GridView1.Rows[OneRank.Value];
Label lRank = (Label)gRow.FindControl("lblRank");
lRank.Text = (ir + 1).ToString();
ir--;
}
This is successfully shows ranking in the template field how you showed me in your example, but:
when template filed uploads with ranking numbers which is this column in Gridview: Convert.ToInt32(GridView1.Rows[t].Cells[2].Text)
and I want to access it:
for (int t = 0; t < GridView1.Rows.Count; t++)
{
if (Convert.ToInt32(GridView1.Rows[t].Cells[2].Text) < 5 )
{
labelX.Text = "message";
}
}
it throws exeption : input format is not in correct format…
If I use
Convert.ToString(GridView1.Rows[t].Cells[2].Text)
format is does not throw exception but cant access the integeres(ranking numbers), if I try to convert it into
Convert.ToInt32(GridView1.Rows[t].Cells[2].Text)`
it throws the exception….
3
Answers
Probably the string value in the cell is not a valid representation of a int, the Parse method will throw a FormatException.
can you provide us with the value that you are trying to convert?
Try using the TryParse method instead.
So, lets MAJOR back up the truck here. We don’t have blank values for rank!!
I mean, right off the bat, alarm bells should be going off here in a HUGE HUGE way!!
AND THEN the whole thing is crashing in a MASSIVE ball of flames, since you now seeing "" or empty for the rank. Now we have a bunch of people offing code HOW to deal with blank values WHEN we NEVER are to have blanks!!!
Talk about running down the wrong path!!!
The issue to solve here NOT how to deal with blanks, but discover WHY are we seeing a blank value!!!
Perhaps I am really wasting my time here. I FLAT OUT CRYSTRAL CLEAR stated in my last post this:
Now, WHY ARE YOU NOT reading my advice here?
so, trying to pull/use/get/grab the rank value from the cells[] collection WILL NEVER work!!!
if the value from the gv row is in a template column, then we can NOT use the cells collection.
However, lets not use the gv to deal with ranking anyway. It far better to simple avoid this issue.
so, lets assume this markup:
And thus we have this code now:
Note HOW we are doing the ranking on the data, and NOT the GV.
So, we now see this:
So, note how it was MUCH better to add a "Rank" colum to the data table.
we then run our rank code against the data, and NOT the GV.
Now, say for some reason that we want say the first 5 to be a light blue, and then anything >5 to be a darker blue.
Well, we then would use the row data bound event, and we can do this, and ONCE again, we don’t even have to use the gv data, but we can use + enjoy the data row value used here. (so, no messy type conversions required!!!).
So, say this code:
And now we see/get this:
So the real moral of the story here?
As I stated, we want to try and work with the data, and NOT against the GV.
However, for the sake of learning, lets PULL the rank value from the GV, and in above, the rank value is a templated column, NOT a databound column.
So, remember that rule:
So, our row data bound event to fomrat the column would now become this:
so, if we are/were/going to/think about/will/entertain the idea of grabbing the value from the gv row? Then above shows how we do this, since as noted, ANY AND ALL templated columns require us to use find control for each row, grab the control type, and then pull the value from the control – we can’t use cells collection anymore for such templated columns.
You are doing this:
You can try to get the text like this:
You’ll that, it’s empty, blank. So, which means, you’re actually converting a blank string into number.
By the way, you code seems a bit mis-alignment, use Ctrl+K+D to align your code.