Asp.net, GridView and Linq on Array of objects

J

James

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 = 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.



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)
{

this.GridView1.DataSource = Utils.GetGlobalListOfOutlookContacts();
this.GridView1.DataBind();
}

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

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

}

My Utils file is defined as:

public static class Utils
{
public static readonly String SPACE = " ";
private static List<AContact> GlobalListOfOutLookContacts = new List<AContact>()
{
new AContact {ContactId=1388, FirstName = "Kriss", LastName = "Bellamy", Company="Oracle", E_mail="(e-mail address removed)", Location="5100 Town Cntr Circle"},
new AContact {ContactId=1389, FirstName = "Ada", LastName = "peterson", Company="Oracle", E_mail="(e-mail address removed)", Location="1364 Park Street"},
new AContact {ContactId=1390, FirstName = "Paul", LastName = "wellby", Company="Oracle", E_mail="(e-mail address removed)", Location="54 State St Suite 1000"},
new AContact {ContactId=1391, FirstName = "Damien", LastName = "Johnson", Company="Google", E_mail="(e-mail address removed)", Location="1 Atlon Court Paris"}
};

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

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

}
return retVal;
}

public static void MarkPrivate(int cid)
{
var ContactList = (from contacts in GlobalListOfOutLookContacts where (contacts.ContactId == cid) select contacts).First();
ContactList.IsPrivateContact = true;
}
}
 
D

David R. Longnecker

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?).
public static void MarkPrivate(int cid)
{
var ContactList = (from contacts in
GlobalListOfOutLookContacts where (contacts.ContactId == cid) select
contacts).First();
ContactList.IsPrivateContact = true;
}

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
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top