Search code examples
c#asp.netcachingobjectdatasourcedata-retrieval

GridView and objectDataSource


I have a grid view that bounded to an object data source that select data from type of student

public class student
{
   int id;
   string name;
   int age;

   public List<students> GetAllStudents()
   {
       // Here I'm retrieving a list of student from Database
   }
}

In the UI Control ascx

<asp:GridView ID="MyGrid" runat="server" 
              DataSourceID="MyDataSource" 
              OnRowCommand="MyGrid_RowCommand">
</asp:GridView>

<asp:ObjectDataSource ID="MyDataSource" runat="server" 
    TypeName="student"
    SelectMethod="GetAllStudents">

In the UI Control code behind

protected void MyGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
     // Here I want to get the list of students from my gridview
}

I want to retrieve the list of data that shown in the Grid to be able to check on the age value of last student in the list

please help me as soon as you can

Thanks in Advance


Solution

  • I have found it

    I can access MyDataSource.Select() method directly and I will get a list of my objects

    protected void MyGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
         List<student> lst =(List<student>)MyDataSource.Select();
    }