How to get a basic Gridview data source?

I have a gridview in my asp.net web app under 3.5. I bind gridview to the list. Inside the grid there are update and delete functions that work fine. But my save functions, for which I decided to extract the list from the data source, and then through the loop, I will introduce a new list into the database. But when I tried to get it in the usual way, it returned me zero.

I tried the following ways to return a list.

1. List<MyClass> list = (List<MyClass>gv.DataSource);
2. List<MyClass> list = gv.DataSource as List<MyClass>;
3. IDataSource idt = (IDataSource)gv.Datasource;
   List<MyClass> list = (List<MyClass>)idt;

But no luck every time I got zero.

+3
source share
3 answers

You cannot return a data source when it is bound and the page is being served. You have several methods available to you to save the data source:

  • ​​Session
  • ​​ViewState
  • , .
  • , (, Session, ViewState ..)

.

, - , . , "", (List), , GUI. GridView , .

+8

gridview .

- :

List<Categories> myList = new List<Categories>();
foreach (GridViewRow row in grdCategory.Rows)
{
    Categories newCat = new Categories();
    Label catID = row.FindControl("lblCatID") as Label;
    newCat.catID = Convert.ToInt32(catID.Text);
    ...
    ...
    myList.Add(newCat);
}

, , -.

+3

As soon as the list is linked and the web page is displayed, the DataSource has disappeared. You need to recreate the data source and access a specific object using the primary key.

0
source

Source: https://habr.com/ru/post/1795821/


All Articles