Solved

How to filter/sort with DataTable

Posted on 2011-03-21
4
270 Views
Last Modified: 2012-05-11
This code lists all names in the gridview. I need to filter/select a specific name "XNAME" and display just this "XNAME" on the gridview... Can u please help me accomplish this?




    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = GetDataTable(@"c:\fans.csv");
            GridView1.DataBind();
        }
    }


private DataTable GetDataTable(string textFile)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Age");
        dt.Columns.Add("State");

        if (File.Exists(textFile))
        {
            using (StreamReader sr = new StreamReader(textFile))
            {
                sr.ReadLine();

                while (!sr.EndOfStream)
                {

                    string value = sr.ReadLine();
   
                    if (!string.IsNullOrEmpty(value))
                    {
                        string[] values = value.Split(new char[] { ';' });

                        if (values.Length > -1)
                        {

                            DataRow row = dt.NewRow();
                            row["Name"] = values[0];
                            row["Age"] = values[1];
                            row["State"] = values[2];
                            dt.Rows.Add(row);
                        }
                    }
                }
            }
        }

        return dt;
    }
0
Comment
Question by:portalvale
4 Comments
 
LVL 13

Expert Comment

by:agarwalrahul
Comment Utility
0
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Hi,

You can do it like this method, you just send the name you want to filter
private void FilterDataTable(string strName)
        {
            DataTable dtFilter = new DataTable();
            dtFilter.Columns.Add("Name");
            dtFilter.Columns.Add("Age");
            dtFilter.Columns.Add("State");

            DataRow[] drFiltered;
            DataTable dtActual;

            dtActual = (DataTable)GridView1.DataSource;
            drFiltered = dtActual.Select("Name = '" + strName + "'");
            for (int i = 0; i < drFiltered.Count(); i++)
            {
                dtFilter.ImportRow(drFiltered[i]);
            }

            GridView1.DataSource = dtFilter;
        }

Open in new window

0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
Comment Utility
0
 
LVL 11

Accepted Solution

by:
Kusala Wijayasena earned 500 total points
Comment Utility
Do like this

GridView1.DataSource = GetDataTable(@"c:\fans.csv").Select("Name = 'XNAME'");
GridView1.DataBind();

Open in new window


-Kusala
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now