Convert a 'System.Collections.Generic.List' object to 'System.Data.Dataset'?

Asked

Viewed 2,184 times

4

Cannot convert an object of type 'System.Collections.Generic.List in type 'System.Data.Dataset'?

After making a report query, I am storing the data in a list and showing the data on the screen, the more I have an option to export the data to the Excel, so not to repeat the query I am saving the data in a session.

Session:

repeater_teste.DataSource = retorno;
Session["Ultimo_Relatorio"] = retorno

In the export option I am doing this way (generating error):

 DataSet ds = (DataSet)Session["Ultimo_Relatorio"];
 DataGrid dg = new DataGrid();
 dg.AllowPaging = false;
 dg.DataSource = ds.Tables[0];
 dg.DataBind();

I found this article that said it was possible.

  • Doubt: why not simply set the list to Binding a Dataset?

  • @jbueno, show me how, put an example please

  • Okay, there. Have a good time!

  • The article talks about storing the DS in Session and not cast from List to Dataset. By the error message it seems that the problem is cast. Supplement the question with the part of the code you store the DS in Session.

  • @Pagotti, adjusted question. more complete

1 answer

4

Tip

I honestly don’t see a good reason to do that. I mean, it’s possible to just make a list Binding of a DataGridView, then why take the trouble to make all this conversion?

Ex.:

dg.DataSource = minhaLista;

Answer

Anyway, here’s a code that does exactly what you need

public static DataSet ToDataSet<T>(this IList<T> list)
{
    Type elementType = typeof(T);
    DataSet ds = new DataSet();
    DataTable t = new DataTable();
    ds.Tables.Add(t);

    // Adicionar uma coluna para propriedade pública em T
    foreach (var propInfo in elementType.GetProperties())
    {
        Type ColType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;

        t.Columns.Add(propInfo.Name, ColType);
    }

    foreach (T item in list)
    {
        DataRow row = t.NewRow();

        foreach (var propInfo in elementType.GetProperties())
        {
            row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value;
        }

        t.Rows.Add(row);
    }

    return ds;
}
  • i cannot use the "Datasource" asks for a reference but it already exists "using System.Data;"

  • @itasouza Young, I mixed up the names. See the issue.

  • then. this answer I already had "dg.Datasource = minhalista;" only that I need to do a new query only to export to excel, I wanted to use what was already in memory

  • All right, but my answer has exactly what you ask for at the end. The beginning is just a hint.

  • Plus I’ll have to make a new query to have "dg.Datasource = mylist;" ? or give to leave a published variable ?

  • I don’t understand what you mean @itasouza

  • I have a query of data that already brings me the return of the data shown on the screen to the user, I am saving the return of the query of the data in a Session, because if the user click exports to excel, I will need to make a new query of the data, so in your example I have to do the query again because I won’t be able to use the content that is in memory. @jbueno

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.