all groups > asp.net > february 2008 >
You're in the

asp.net

group:

Asp.net, GridView and Linq on Array of objects



Asp.net, GridView and Linq on Array of objects James
2/29/2008 8:14:31 PM
asp.net: I've got a GridView control on a simple web page in ASP.net 3.5.

I've an Array of Employees. An Employee has First Name, Last Name, =
IsActiveEmployee etc

My GridView control is Bounded to the Array: DataSource =3D myarray, =
gridview1.DataBind()

I have template column with holds a link button

when the button is clicked, I've got a method which will set the =
selected

employee "IsActiveEmployee" flag to false.

My problem is that when the page reloads and binds, the array returned =
is NOT change - it is the same array. =20



Can somebody please help?

My Page file is defined as:

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
=20
this.GridView1.DataSource =3D =
Utils.GetGlobalListOfOutlookContacts();
this.GridView1.DataBind();
}

protected void MarkAsPrivate(int id)
{
int tmpId =3D id;
Utils.MarkPrivate(tmpId);
}

protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
object o =3D e.CommandArgument;
String s =3D Convert.ToString(o);
int tmpId =3D Convert.ToInt32(s); =20
Utils.MarkPrivate(tmpId);
}

}

My Utils file is defined as:

public static class Utils
{
public static readonly String SPACE =3D " ";
private static List<AContact> GlobalListOfOutLookContacts =3D =
new List<AContact>()
{
new AContact {ContactId=3D1388, FirstName =3D "Kriss", =
LastName =3D "Bellamy", Company=3D"Oracle", =
E_mail=3D"kriss.bellamy@oracle.com", Location=3D"5100 Town Cntr =
Circle"},
new AContact {ContactId=3D1389, FirstName =3D "Ada", =
LastName =3D "peterson", Company=3D"Oracle", =
E_mail=3D"ada.peterson@oracle.com", Location=3D"1364 Park Street"},
new AContact {ContactId=3D1390, FirstName =3D "Paul", =
LastName =3D "wellby", Company=3D"Oracle", =
E_mail=3D"paul.wellby@oracle.com", Location=3D"54 State St Suite 1000"},
new AContact {ContactId=3D1391, FirstName =3D "Damien", =
LastName =3D "Johnson", Company=3D"Google", =
E_mail=3D"damien.johnson@oracle.com", Location=3D"1 Atlon Court Paris"}
};

public static List<AContact> GetGlobalListOfOutlookContacts()
{
var ContactList =3D from contacts in =
GlobalListOfOutLookContacts where contacts.IsPrivateContact !=3D true =
select contacts;

List<AContact> retVal =3D new List<AContact>();
foreach (AContact c in ContactList)
{
retVal.Add(c);

}
return retVal;
}

public static void MarkPrivate(int cid)
{
var ContactList =3D (from contacts in =
GlobalListOfOutLookContacts where (contacts.ContactId =3D=3D cid) select =
contacts).First();
ContactList.IsPrivateContact =3D true;
}
Re: Asp.net, GridView and Linq on Array of objects David R. Longnecker
2/29/2008 9:36:09 PM
James-

When you query out an object using LINQ and then update it, you must submit
it back to the data source.

I'm assuming your LinkButton1_Command is what is toggling your IsActiveEmployee
(IsPrivateContact?).

[quoted text, click to view]

After you update your ContactList object here, add in:

TheNameOfYourDataContextObject.SubmitChanges(); // If you've build your
own Update method, you could fire that off instead.
gridview1.DataBind(); // To rebind your data to the updated data source.

HTH.

-dl

--
David R. Longnecker
http://blog.tiredstudent.com

[quoted text, click to view]

AddThis Social Bookmark Button