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();
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();