how to update an edited dataview item

J

jrl

I have a working situation in which a datatable is filled with data, and
then I make a dataview, in order to sort it. Additionally, I want to change
the appearance of strings within the dataview, by shortening those which are
over 20 characters long. To do this, I take a copy of the particular string,
I shorten them, and then I try to write them back into the dataview. This
last step is not working. How can I alter the content of the dataview? I
don't want to alter the underlying datatable from which the dataview is
derived.

Here a shortened sample, in which I've commented the part that doesn't work.

DataTable dt = GetData().Tables[0]; // pulls up data ok

DataView dv = new DataView(dt); // because dataviews can sort

dv.Sort = sortExpression +" " + direction;
dv.AllowEdit = true;

// trim the proper field in all rows

for(int i=0;i<dv.Table.Rows.Count;i++)
{
string s = dv.Table.Rows.ItemArray[2].ToString();

if (s.Length > 20)
{
s = "..." + s.Substring(s.Length - 20); // for last 20
characters of referrer
dv.Table.Rows.BeginEdit();
dv.Table.Rows.ItemArray[2]= s; // this is the step that
doesn't work.
dv.Table.Rows.EndEdit();
dv.Table.Rows.AcceptChanges();
}

}

GridView1.DataSource = dv;
GridView1.DataBind();
 
R

RobinS

I could be wrong (and I'm sure others will chime in if I am ;-), but I think
you need to update the underlying data table, not the data view. I believe a
dataview is exactly that -- a view.

RobinS.
GoldMail, Inc.
 
L

Linda Liu[MSFT]

Hi Jrl,

I performed a test based on your description and sample code and did
reproduce the problem on my side.
dv.Table.Rows.ItemArray[2]= s; // this is the step that doesn't

work.

The get accessors of the DataRow.ItemArray property returns an array
containing all the values of the row. Changing the item values of the
returned array doesn't change the column values of the DataRow. This is the
reason why the above line of code doesn't work.

To set the column values of a DataRow, you can use the DataRow.Item
property. For example:

// in C#, the Item property is implemented by the operator [ ]
dv.Table.Rows[2] = s;

Please try my suggestion and let me know the result.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
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.
 
J

jrl

Thanks for the clarification Robin, I see I'll need to make a copy of the
datatable. Views seem to just be a way to show a filtered subset.
 

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