Altering value to be returned by DataRow.Item

J

Joe

I'm looking for a suggestion on how to do the following:

We have a case when values returned by DataTable.Rows[r][c] should be
different than what is actually stored.
For example:

DataTable dt = new DataTable();
dt.Columns.Add("Column1");

dt.Rows.Add(new object[] {"Test Value") );

When reading the code I would like to do a lookup and see if there is a
replacement value for "Test Value" and if so return that. The replacement
value would be stored in another table or anywhere that makes sense.

Ideally it would be nice to create my own DataRow and override the Item
property so I can use my DataTable in say a DataGridView and also get the
replacement value instead of the stored value if one existed.

I would like to do the following (but of course I can't because Item is not
overloadable in the DataRow):

public class MyDataRow : DataRow
{
private object replacementValue = "Some other value";
...

public object this[int index]
{
get
{
if (replacementvalue != null)
return replacementvalue;

return base[index];
}
}
}

So in a DataGridView or any other object that was reading dt.Rows[r][c]
would get "Some other value" instead of "Test Value"

Is there anyway to return a different value for the DataRow? Maybe there is
an event I don't know about that would allow me to do this.

Thanks for any help,
Joe
 
J

Joe

Hi Calvin,

The problem with this approach is that I would need to have duplicate
records in my DataTable except for a single value.

-Joe

Calvin Luttrell from Projecthunder said:
Joe,

If it were me I would use a IIF or case statement to return the value
you're looking for.


Calvin Luttrell

How e-commerce is done.

http://blog.projectthunder.com
http://www.projectthunder.com




Joe said:
I'm looking for a suggestion on how to do the following:

We have a case when values returned by DataTable.Rows[r][c] should be
different than what is actually stored.
For example:

DataTable dt = new DataTable();
dt.Columns.Add("Column1");

dt.Rows.Add(new object[] {"Test Value") );

When reading the code I would like to do a lookup and see if there is a
replacement value for "Test Value" and if so return that. The replacement
value would be stored in another table or anywhere that makes sense.

Ideally it would be nice to create my own DataRow and override the Item
property so I can use my DataTable in say a DataGridView and also get the
replacement value instead of the stored value if one existed.

I would like to do the following (but of course I can't because Item is
not overloadable in the DataRow):

public class MyDataRow : DataRow
{
private object replacementValue = "Some other value";
...

public object this[int index]
{
get
{
if (replacementvalue != null)
return replacementvalue;

return base[index];
}
}
}

So in a DataGridView or any other object that was reading dt.Rows[r][c]
would get "Some other value" instead of "Test Value"

Is there anyway to return a different value for the DataRow? Maybe there
is an event I don't know about that would allow me to do this.

Thanks for any help,
Joe
 
K

Kevin Yu [MSFT]

Hi Joe,

We cannot override DataRow.Item. I think there will be no graceful way
achieve this. In my opinion, I will wrap a class around the whole
DataTable, checking for the returned value and query for new one.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
K

Kevin Yu [MSFT]

Hi Joe,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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