467,135 Members | 1,140 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,135 developers. It's quick & easy.

getting value from cell

Hi,
Easy question. It seems to me that I'm following the examples correctly,
but apparently I'm not. I'm trying to retrieve the data from a row called
"row" and a column called "File". This is what I have: (xFile is the int
value in column File and tCat is the table)

First I try:
xFile = int.Parse((tCat.Rows[row]["File"]).ToString());

Another example I found:
DataRow dRow = tCat.Rows[row];
xFile = int.Parse(dRow["File"].ToString());

Instead of getting the value back, I'm consistently getting the row, or index.
Thanks!!!
Mel
Nov 17 '05 #1
  • viewed: 1859
Share:
6 Replies
Hi Mel,

Both those examples look fine to me.

Are you sure that the "File" column contains int values?

You may also omit the ".ToStirng()" bit.

Best,
Andre

Nov 17 '05 #2
Mel,

What is the value that should be in the file column? If it is not an
integer, I imagine that you would get exceptions in these cases. The "row"
variable is an index of a row in the table.

Also, the value in the column does not need to be parsed. Rather, you
can just cast to the type you need (assuming that there is a cast
available), like so:

xFile = (int) tCat.Rows[row]["File"];

However, something tells me that you don't want to cast to an integer,
but rather, to a string.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
Hi,
Easy question. It seems to me that I'm following the examples correctly,
but apparently I'm not. I'm trying to retrieve the data from a row called
"row" and a column called "File". This is what I have: (xFile is the int
value in column File and tCat is the table)

First I try:
xFile = int.Parse((tCat.Rows[row]["File"]).ToString());

Another example I found:
DataRow dRow = tCat.Rows[row];
xFile = int.Parse(dRow["File"].ToString());

Instead of getting the value back, I'm consistently getting the row, or
index.
Thanks!!!
Mel

Nov 17 '05 #3
The value in the File column is an integer, which is loaded from an xml file
(it's the attribute (ID) of whichever file). I load the table using a loop
containing:

tCat.Rows.Add(new object[]{nodeF.Item(i).Attributes["ID"].Value, ...});

where nodeF is the xmlNodeList = xml.GetElementsByTagName("File");

Actually, the code worked fine back when I would just load straight from the
xml file and retrieve the File value. But now, a column header is clicked so
the files are sorted alphabetically. When I click, I have no problem getting
the new row number, but I can't get the File value of that row. I can see
the numbers in the File column, and they're correct, I just can't retrieve
them.

Any suggestions?
Mel

"Nicholas Paldino [.NET/C# MVP]" wrote:
Mel,

What is the value that should be in the file column? If it is not an
integer, I imagine that you would get exceptions in these cases. The "row"
variable is an index of a row in the table.

Also, the value in the column does not need to be parsed. Rather, you
can just cast to the type you need (assuming that there is a cast
available), like so:

xFile = (int) tCat.Rows[row]["File"];

However, something tells me that you don't want to cast to an integer,
but rather, to a string.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
Hi,
Easy question. It seems to me that I'm following the examples correctly,
but apparently I'm not. I'm trying to retrieve the data from a row called
"row" and a column called "File". This is what I have: (xFile is the int
value in column File and tCat is the table)

First I try:
xFile = int.Parse((tCat.Rows[row]["File"]).ToString());

Another example I found:
DataRow dRow = tCat.Rows[row];
xFile = int.Parse(dRow["File"].ToString());

Instead of getting the value back, I'm consistently getting the row, or
index.
Thanks!!!
Mel


Nov 17 '05 #4
Mel,

I assume this is attached to a grid of some kind. Because of this, the
grid is bound to the DataView returned by the DefaultView property on the
DataTable. This returns the view that the grid is bound to. When you
change the sort order, it is on this view, not the data table itself (those
are never ordered, or at least, there should not be expectation of ordering
unless you know how it was populated).

What you want to do is use the view returned from the DefaultView
property, like so (you might have to tweak this):

// Get the view.
DataView view = tCat.DefaultView;

// Get the id.
xFile = (int) view.Rows[row]["File"];

That should work.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
The value in the File column is an integer, which is loaded from an xml
file
(it's the attribute (ID) of whichever file). I load the table using a
loop
containing:

tCat.Rows.Add(new object[]{nodeF.Item(i).Attributes["ID"].Value, ...});

where nodeF is the xmlNodeList = xml.GetElementsByTagName("File");

Actually, the code worked fine back when I would just load straight from
the
xml file and retrieve the File value. But now, a column header is clicked
so
the files are sorted alphabetically. When I click, I have no problem
getting
the new row number, but I can't get the File value of that row. I can see
the numbers in the File column, and they're correct, I just can't retrieve
them.

Any suggestions?
Mel

"Nicholas Paldino [.NET/C# MVP]" wrote:
Mel,

What is the value that should be in the file column? If it is not an
integer, I imagine that you would get exceptions in these cases. The
"row"
variable is an index of a row in the table.

Also, the value in the column does not need to be parsed. Rather,
you
can just cast to the type you need (assuming that there is a cast
available), like so:

xFile = (int) tCat.Rows[row]["File"];

However, something tells me that you don't want to cast to an
integer,
but rather, to a string.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
> Hi,
> Easy question. It seems to me that I'm following the examples
> correctly,
> but apparently I'm not. I'm trying to retrieve the data from a row
> called
> "row" and a column called "File". This is what I have: (xFile is the
> int
> value in column File and tCat is the table)
>
> First I try:
> xFile = int.Parse((tCat.Rows[row]["File"]).ToString());
>
> Another example I found:
> DataRow dRow = tCat.Rows[row];
> xFile = int.Parse(dRow["File"].ToString());
>
> Instead of getting the value back, I'm consistently getting the row, or
> index.
> Thanks!!!
> Mel
>
>


Nov 17 '05 #5
Thanks so much!!!
Mel

"Nicholas Paldino [.NET/C# MVP]" wrote:
Mel,

I assume this is attached to a grid of some kind. Because of this, the
grid is bound to the DataView returned by the DefaultView property on the
DataTable. This returns the view that the grid is bound to. When you
change the sort order, it is on this view, not the data table itself (those
are never ordered, or at least, there should not be expectation of ordering
unless you know how it was populated).

What you want to do is use the view returned from the DefaultView
property, like so (you might have to tweak this):

// Get the view.
DataView view = tCat.DefaultView;

// Get the id.
xFile = (int) view.Rows[row]["File"];

That should work.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
The value in the File column is an integer, which is loaded from an xml
file
(it's the attribute (ID) of whichever file). I load the table using a
loop
containing:

tCat.Rows.Add(new object[]{nodeF.Item(i).Attributes["ID"].Value, ...});

where nodeF is the xmlNodeList = xml.GetElementsByTagName("File");

Actually, the code worked fine back when I would just load straight from
the
xml file and retrieve the File value. But now, a column header is clicked
so
the files are sorted alphabetically. When I click, I have no problem
getting
the new row number, but I can't get the File value of that row. I can see
the numbers in the File column, and they're correct, I just can't retrieve
them.

Any suggestions?
Mel

"Nicholas Paldino [.NET/C# MVP]" wrote:
Mel,

What is the value that should be in the file column? If it is not an
integer, I imagine that you would get exceptions in these cases. The
"row"
variable is an index of a row in the table.

Also, the value in the column does not need to be parsed. Rather,
you
can just cast to the type you need (assuming that there is a cast
available), like so:

xFile = (int) tCat.Rows[row]["File"];

However, something tells me that you don't want to cast to an
integer,
but rather, to a string.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
> Hi,
> Easy question. It seems to me that I'm following the examples
> correctly,
> but apparently I'm not. I'm trying to retrieve the data from a row
> called
> "row" and a column called "File". This is what I have: (xFile is the
> int
> value in column File and tCat is the table)
>
> First I try:
> xFile = int.Parse((tCat.Rows[row]["File"]).ToString());
>
> Another example I found:
> DataRow dRow = tCat.Rows[row];
> xFile = int.Parse(dRow["File"].ToString());
>
> Instead of getting the value back, I'm consistently getting the row, or
> index.
> Thanks!!!
> Mel
>
>


Nov 17 '05 #6
Although in case anyone ever needs this, the exact syntax that works is
xFile = int.Parse(dv[row].Row["File"].ToString());

"melanieab" wrote:
Thanks so much!!!
Mel

"Nicholas Paldino [.NET/C# MVP]" wrote:
Mel,

I assume this is attached to a grid of some kind. Because of this, the
grid is bound to the DataView returned by the DefaultView property on the
DataTable. This returns the view that the grid is bound to. When you
change the sort order, it is on this view, not the data table itself (those
are never ordered, or at least, there should not be expectation of ordering
unless you know how it was populated).

What you want to do is use the view returned from the DefaultView
property, like so (you might have to tweak this):

// Get the view.
DataView view = tCat.DefaultView;

// Get the id.
xFile = (int) view.Rows[row]["File"];

That should work.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"melanieab" <me*******@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
The value in the File column is an integer, which is loaded from an xml
file
(it's the attribute (ID) of whichever file). I load the table using a
loop
containing:

tCat.Rows.Add(new object[]{nodeF.Item(i).Attributes["ID"].Value, ...});

where nodeF is the xmlNodeList = xml.GetElementsByTagName("File");

Actually, the code worked fine back when I would just load straight from
the
xml file and retrieve the File value. But now, a column header is clicked
so
the files are sorted alphabetically. When I click, I have no problem
getting
the new row number, but I can't get the File value of that row. I can see
the numbers in the File column, and they're correct, I just can't retrieve
them.

Any suggestions?
Mel

"Nicholas Paldino [.NET/C# MVP]" wrote:

> Mel,
>
> What is the value that should be in the file column? If it is not an
> integer, I imagine that you would get exceptions in these cases. The
> "row"
> variable is an index of a row in the table.
>
> Also, the value in the column does not need to be parsed. Rather,
> you
> can just cast to the type you need (assuming that there is a cast
> available), like so:
>
> xFile = (int) tCat.Rows[row]["File"];
>
> However, something tells me that you don't want to cast to an
> integer,
> but rather, to a string.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
> "melanieab" <me*******@discussions.microsoft.com> wrote in message
> news:F2**********************************@microsof t.com...
> > Hi,
> > Easy question. It seems to me that I'm following the examples
> > correctly,
> > but apparently I'm not. I'm trying to retrieve the data from a row
> > called
> > "row" and a column called "File". This is what I have: (xFile is the
> > int
> > value in column File and tCat is the table)
> >
> > First I try:
> > xFile = int.Parse((tCat.Rows[row]["File"]).ToString());
> >
> > Another example I found:
> > DataRow dRow = tCat.Rows[row];
> > xFile = int.Parse(dRow["File"].ToString());
> >
> > Instead of getting the value back, I'm consistently getting the row, or
> > index.
> > Thanks!!!
> > Mel
> >
> >
>
>
>


Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Brian Alexander | last post: by
2 posts views Thread by Graham Y | last post: by
2 posts views Thread by niceguy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.