Row Cell

J

Jim Heavey

Hello, Coming from VB, accessing cells is a little different in C#.

If I have a datatable, I see the ItemArray propery, which gives you an
array of the items in a row (right?), but it does not allow you to access
the individual cell. Do I have to get the ItemArray and access the
individual cell from that, or is there another way. In VB there was the
"item" property.

dt.rows[myRow].item[2] = "Fred"

If I have a Datarow is there a property that tells me the index of that row
in a DataTable?

Likewise, if I have a DataRowView Row, is there a property which tells me
the index of that row?

Thanks in advance for your assistance!!!!
 
T

Teemu Keiski

Hi,

in C# you just access it via indexer:

dt.rows[myRow][2] = "Fred"

(assumes myRow is the index)

Unfortunately as far as I know, you can't get the index with DataRows
unless you loop all rows through as usually it would be like IndexOf
property in DataRowCollection (Rows property) as it is with some other
collection. IndexOf is part of IList interface but DataRowCollection
implements only ICollection so it does not have the IndexOf member as it
doesn't implement IList.

With DataView it is bit different as it implements IList interface. So with
DataRowView you get the index by casting the DataView to IList and assigning
current DataRowView instance to the IndexOf method of casted DataView. THis
technique is also kind of answer to the problem that DataRowCollection does
not have IndexOf method (way to get the index) as every DataTable can have
corresponding DataView.
 
T

Teemu Keiski

Sorry typo:

dt.Rows[myRow][2]="Fred";

(C# is case-sensitive)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


Teemu Keiski said:
Hi,

in C# you just access it via indexer:

dt.rows[myRow][2] = "Fred"

(assumes myRow is the index)

Unfortunately as far as I know, you can't get the index with DataRows
unless you loop all rows through as usually it would be like IndexOf
property in DataRowCollection (Rows property) as it is with some other
collection. IndexOf is part of IList interface but DataRowCollection
implements only ICollection so it does not have the IndexOf member as it
doesn't implement IList.

With DataView it is bit different as it implements IList interface. So with
DataRowView you get the index by casting the DataView to IList and assigning
current DataRowView instance to the IndexOf method of casted DataView. THis
technique is also kind of answer to the problem that DataRowCollection does
not have IndexOf method (way to get the index) as every DataTable can have
corresponding DataView.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist




Jim Heavey said:
Hello, Coming from VB, accessing cells is a little different in C#.

If I have a datatable, I see the ItemArray propery, which gives you an
array of the items in a row (right?), but it does not allow you to access
the individual cell. Do I have to get the ItemArray and access the
individual cell from that, or is there another way. In VB there was the
"item" property.

dt.rows[myRow].item[2] = "Fred"

If I have a Datarow is there a property that tells me the index of that row
in a DataTable?

Likewise, if I have a DataRowView Row, is there a property which tells me
the index of that row?

Thanks in advance for your assistance!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top