RachitSharma
RachitSharma

Reputation: 589

How to use generic list in place of dataset/datable in asp.net?

I want to copy all my data from database to generic list to which i will bind to gridview. but i won't be able to do it . what i am doing is as :

Collapse | Copy Code
public List<string>   fillgridviewDAL()
{
    List<string> NameList = (from r in dataset.Tables["MyTableName"].AsEnumerable()
                             select r.Field<string>("name") + r.Field<string>("age") + r.Field<string>("salary") + r.Field<string>("city")).ToList();
    return NameList;
}

when i see contents in dataset it shows full table but when i use generic list using above method it concat columns and gives a resultant row at particular index. In short it gives me output in string form. Than how can i bind the gridview to my generic list.

Upvotes: 0

Views: 1591

Answers (3)

jbl
jbl

Reputation: 15413

Not sure I understood what you want to achieve. Maybe a List of Dictionaries can do the trick. Not tried though. Hope this will help

public List<Dictionary<String, object>>   fillgridviewDAL()
{
List<Dictionary<String, object>> rows =
    dataset.Tables["MyTableName"]
                .AsEnumerable()
                .Select(
                    row =>
                       row.Table.Columns
                          .Cast<DataColumn>()
                          .ToDictionary(column => column.ColumnName,
                                        column => row[column]))
                .ToList();

    return rows ;
}

Alternatively, if you are only interested in the values, you may go for List<List<Object>>, like this :

public List<List<object>> fillgridviewDAL()
{
    DataSet dataset = null;
    List<List<object>> rows =
        dataset.Tables["MyTableName"]
                    .AsEnumerable()
                    .Select(
                        row =>
                        row.Table.Columns
                                .Cast<DataColumn>()
                                .Select(column => row[column]).ToList())
                    .ToList();

    return rows;
}

Upvotes: 0

Damith
Damith

Reputation: 63065

below will set the DataSource of grid with selected columns

gridview.DataSource = dataset.Tables["MyTableName"]
                       .DefaultView.ToTable(false, "name", "age", "salary", "city");

if you are selecting columns dynamically, assume column names are in string[] called columns

gridview.DataSource =dataset.Tables["MyTableName"].DefaultView.ToTable(false, columns);

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

You should create class that will hold your row data:

public class Item
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Salary { get; set; }
    public string City { get; set; }
}

And then return List<Item>:

public List<Item> fillgridviewDAL()
{
    List<Item> NameList = (from r in dataset.Tables["MyTableName"].AsEnumerable()
                           select new Item {
                               Name = r.Field<string>("name"),
                               Age = r.Field<string>("age"),
                               Salary = r.Field<string>("salary"),
                               City = r.Field<string>("city")
                           }).ToList();
    return NameList;
}

Upvotes: 1

Related Questions