Showing posts with label C# List. Show all posts
Showing posts with label C# List. Show all posts

Thursday, May 3, 2012

GridView Filter using LINQ

We shall see, how to implement a Client side GridView Filter using LINQ, without hitting the database. We will be using a C# List as the data source to the Grid View, instead we can also use a DataTable.

To know more about binding a C# List to a GridView refer the post Binding a C# List to a GridView control

First let us create a class whose objects will be stored in the List (refer post Binding a C# List to a GridView control)


Next, let us create a list of objects based on our class clsCountry and store them in a List object. Here is the code for the List


List<clsCountry> lstCountry = new List<clsCountry>();

lstCountry.Add(new clsCountry("USA""United States"));

lstCountry.Add(new clsCountry("UK""United Kingdon"));

lstCountry.Add(new clsCountry("IND""India"));

lstCountry.Add(new clsCountry("RUS", "Russia"));
lstCountry.Add(new clsCountry("SA", "South Africa"));
lstCountry.Add(new clsCountry("AUS", "Australia"));

Finally we shall bind the List object lstCountry to a GridView control. Here the code to bind the data.


grdCountry.DataSource = lstCountry;

grdCountry.DataBind();


Now this will bind all the value to the GridView, to filter the values we need to add a LINQ filter and bind the filtered values to the GridView, as follows

var filteredCountries = from c in lstCountry
where c.CountryName.Contains(txtFilter.Text)
select c;

grdCountry.DataSource = filteredCountries;
grdCountry.DataBind();

Here the LINQ expression takes the filter text from the textbox txtFilter.Text, and applies the filter on the List object, the result of the filter expression in bound to the GridView.

In this example we have used a List object, instead we can also use a DataTable and apply the LINQ filter to the DataTable.

That’s it we have filtered the contents of GridView control using LINQ filters.


Related Post
Binding a C# List to a GridView control
What is LINQ?

Binding a C# List to a GridView control

In general we use a DataTable or a DataReader to bind data to a GridView control, today we shall see on how to bind a C# List to a GridView control.


To know more about the C# List object refer to the post C# List

First let us create a class whose objects will be stored in the List, here is the code for the class.



public class clsCountry

{

    public string _CountryCode;

    public string _CountryName;

    //

    public clsCountry(string strCode, string strName)

    {

        this._CountryCode = strCode;

        this._CountryName = strName;

    }

    //

    public string CountryCode

    {

        get {return _CountryCode;}

        set {_CountryCode = value;}

    }

    //

    public string CountryName

    {

        get { return _CountryName; }

        set { _CountryName = value; }

    }

}


Next, let us create a list of objects based on our class clsCountry and store them in a List object. Here is the code for the List

List<clsCountry> lstCountry = new List<clsCountry>();

lstCountry.Add(new clsCountry("USA", "United States"));

lstCountry.Add(new clsCountry("UK", "United Kingdon"));

lstCountry.Add(new clsCountry("IND", "India"));


Finally we shall bind the List object lstCountry to a GridView control. Here the code to bind the data.


grdCountry.DataSource = lstCountry;
grdCountry.DataBind();

Notice that the columns bound to the GridView control are mapped to the Properties of the class CountryCode and CountryName, hence make sure to create properties for every member of the class so that they can be used while binding the data to controls.


That’s it we have bound the contents of a C# List object to a GridView control.

Related Post
1. C# List

2. Binding a C# List to a DropDownList control

Wednesday, April 18, 2012

C# List


C# List

The List class in C# can be used to store typed objects. It is similar to an array but can store objects of custom type.

Arrays can hold objects of the predefined type like Integer, String, Boolean etc, but what if we need to store a list of objects belonging to a custom class which we define, this is where the List class comes into play, we can use the List to store custom objects of any type. Another advantage of the List over arrays is that we need not specify the dimension of the List while declaring, the List can dynamically grow/shrink as and when we add/remove items from the list.
The List can hold both the predefined types like Integer, String, Boolean, etc and custom types defined by us.

The List class comes under the System.Collections.Generic namespace, so make sure to include this namespace whenever you are using List.

using System.Collections.Generic;

Syntax
List<Type> lstObjects = new List<Type>();  

Example

First let us create a class clsCountry, later we shall store objects of this class in the List, here is our custom class.

public class clsCountry
{
    public string _CountryCode;
    public string _CountryName;
    //
    public clsCountry(string strCode, string strName)
    {
        this._CountryCode = strCode;
        this._CountryName = strName;
    }
    //
    public string CountryCode
    {
        get {return _CountryCode;}
        set {_CountryCode = value;}
    }
    //
    public string CountryName
    {
        get { return _CountryName; }
        set { _CountryName = value; }
    }
}

Now let us create a List to store a collection of objects of type clsCountry.
Here is the code.

List<clsCountry> lstCountry = new List<clsCountry>();
lstCountry.Add(new clsCountry("USA", "United States"));
lstCountry.Add(new clsCountry("UK", "United Kingdon"));
lstCountry.Add(new clsCountry("IND", "India"));
//
int nCount = lstCountry.Count();

Now objects of type clsCountry are added to the list lstCountry, the variable nCount will have a value of 3, since we added 3 objects to the List.

That’s it we have seen the advantages of using List with an Example.