protected void CountAlbums_Click(object sender, EventArgs e) { //traversing a GridView display //just like a list view, the only records available to us at this time out of the dataset assigned to the GridView are the rows being displayed. //eg, I have 100 rows, out of all of those records, I am only deal with what I want to display. //IF to deal with all records, No gridview, do Datasets! //create a List<T> to hold the counts of the display // need the using name space to access entity List <ArtistAlbumCounts> Artists = new List <ArtistAlbumCounts>(); //reusable pointers to an instance of the specified class ArtistAlbumCounts item = null; int artistid = 0; //setup the loop to traver the gridview foreach (GridViewRow line in AlbumList.Rows) { //access the artistid //use the select value instead of selected index because list can be sorted. //The only time to use selected index is to determin if something has been selected. artistid = int.Parse((line.FindControl("ArtistList") as DropDownList).SelectedValue); //determine if you have already created a count instance in the list<T> for this artist. //if Not, create a new instance fo the artist and set its count to 1. //if ound, increment the count (+1) //1. search for the art in list<T> //x is the current record of the find opearation is looking at at that instance //=> means for x, do the following: //ArtistID from the list of T, compare to the artistid from the grid view. //what will be returned is either null (not found) or the instance in the list<T> item = Artists.Find(x => x.ArtistId == artistid); if (item == null) { //Create instance, initialize, add it List<T>, item = new ArtistAlbumCounts(); item.ArtistId = artistid; item.AlbumCount = 1; Artists.Add(item); } else { item.AlbumCount++; } } //attach the List<T> (collection) to the display control ArtistAlbumCountList.DataSource = Artists; ArtistAlbumCountList.DataBind(); }
protected void CountAlbums_Click(object sender, EventArgs e) { //traversing a GridView display //the only records available to us at this time // out of the dataset assigned to the GridView // are the row being display //create a List<T> to hold the counts of the display List <ArtistAlbumCounts> Artists = new List <ArtistAlbumCounts>(); //reusable pointer to an instance of the specified class ArtistAlbumCounts item = null; int artistid = 0; //setup the loop to traverse the gridview foreach (GridViewRow line in AlbumList.Rows) { //access the artistid artistid = int.Parse((line.FindControl("ArtistList") as DropDownList).SelectedValue); //determine if you have already created a count // instance in the List<T> for this artist //if NOT, create a new instance for the artist and // set its count to 1 //if found, increment the counter (+1) //search for artist in list<T> //what will be return is either null (not found) // or the instance in the List(T) item = Artists.Find(x => x.ArtistId == artistid); if (item == null) { //Create instance, initialize, add to List<T> item = new ArtistAlbumCounts(); item.ArtistId = artistid; item.AlbumCount = 1; Artists.Add(item); } else { item.AlbumCount++; } } //attach the List<T> (collection) to the display control ArtistAlbumCountList.DataSource = Artists; ArtistAlbumCountList.DataBind(); }
protected void CountAlbums_Click(object sender, EventArgs e) { //traversing a GridView display (counting artists in List<T> within GridView) //the only records available to us at this time out of the data set assigned to the GridView are the rows being displayed (only counts page being shown) //create a List<T> to hold the counts of the display List <ArtistAlbumCounts> Artists = new List <ArtistAlbumCounts>(); //reusable pointer to an instance of the specified class ArtistAlbumCounts item = null; int artistId = 0; //setup the loop to traverse the GridVIew foreach (GridViewRow line in AlbumList.Rows) { //access the artist id artistId = int.Parse((line.FindControl("ArtistList") as DropDownList).SelectedValue); //determine if you have already created a count instance in the List<T> for this artist //if NOT, create a new instance for the artist and set it's count to 1 //if Yes Increment the counter +1 //search for artist in List<T> what will be return will either be null (Not found) or the insance in the List<T> item = Artists.Find(x => x.ArtistId == artistId); if (item == null) { //Create Instance, Innitialize, add to list item = new ArtistAlbumCounts(); item.ArtistId = artistId; item.AlbumCount = 1; Artists.Add(item); } else { item.AlbumCount++; } //Attach List<T> to the display control ArtistAlbumCountList.DataSource = Artists; ArtistAlbumCountList.DataBind(); } }
protected void CountAlbums_Click(object sender, EventArgs e) { // Note: Only available records are the instances on the grids current page List <ArtistAlbumCounts> ArtistList = new List <ArtistAlbumCounts>(); // Reusable pointer to the instance of the class ArtistAlbumCounts item = null; // Iterate through each grid row int artistID = 0; foreach (GridViewRow row in Albums.Rows) { // Current rows artist ID artistID = int.Parse((row.FindControl("Artists") as DropDownList).SelectedValue); // Check if the current item is already accounted for item = ArtistList.Find(x => x.ArtistId == artistID); // Result of check if (item == null) { // Create instance and add it to the list item = new ArtistAlbumCounts(); item.ArtistId = artistID; item.AlbumCount = 1; ArtistList.Add(item); } else { item.AlbumCount++; } } // Attach the list to the display control ArtistAlbumCountList.DataSource = ArtistList; ArtistAlbumCountList.DataBind(); }