7 November 2011

Edit the Gridview in Asp.net with List Collection Example

   Edit the Gridview in Asp.net with List Collection Example

           
       
  ASPX.CS

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    public void BindData()
    {
        peopleGridView.DataSource = GetPersonList();
        peopleGridView.DataBind();
    }

    protected void selectButton_Click(object sender, EventArgs e)
    {
        LinkButton selectButton = (LinkButton)sender;
        //the button is contained in a TableCell which is contained in a GridViewRow
        GridViewRow row = (GridViewRow)selectButton.Parent.Parent;
        //get the row index of the selected row
        int rowIndex = row.RowIndex;

        //call our populate function
        PopulateLabelsByRowIndex(rowIndex);

    }
    protected void peopleGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //get our new selected index
        int rowIndex = e.NewSelectedIndex;

        //call our populate function
        PopulateLabelsByRowIndex(rowIndex);
    }
    private void PopulateLabelsByRowIndex(int rowIndex)
    {
        //assign the row number to our message label
        messageLabel.Text = String.Format("{0}", rowIndex + 1);

        //display the value on column 2 of the selected row
        nameLabel.Text = peopleGridView.Rows[rowIndex].Cells[2].Text;
        //display the value on column 3 of the selected row
        emailLabel.Text = peopleGridView.Rows[rowIndex].Cells[3].Text;
        //display the value on column 4 of the selected row
        ageLabel.Text = peopleGridView.Rows[rowIndex].Cells[4].Text;
    }

    public List GetPersonList()
    {
        //create a new list of person
        List people = new List();
     
        people.Add(new Person(1, "Arun Prakash", "apkmca@gmail.com", 28));
        people.Add(new Person(2, "Sachin", "sachin@Cricketgod.org", 38));
        people.Add(new Person(3, "Ivy Rull", "ivy@devpinoy.org", 24));
        people.Add(new Person(4, "Orlando Rull", "orlando@devpinoy.org", 52));
        people.Add(new Person(5, "Benilda Rull", "benilda@devpinoy.org", 49));
        people.Add(new Person(6, "Ria Rull", "ria@devpinoy.org", 22));
        people.Add(new Person(7, "Renz Rull", "renz@devpinoy.org", 20));

        //return our list
        return people;
    }
}

public class Person
{
 
    public int PersonID
    {
        get;

        set;

    }
    public string Name
    {
        get;

        set;

    }
    public string Email
    {
        get;
 
       set;
     
    }
    public int Age
    {
        get;

        set;
   
    }

    public Person(int personID, string name, string email, int age)
    {
        this.PersonID = personID;
        this.Name = name;
        this.Email = email;
        this.Age = age;
    }
}

No comments:

Post a Comment

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...