Solved

How to filter/sort with DataTable

Posted on 2011-03-21
4
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 13

Expert Comment

by:agarwalrahul
ID: 35186216
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35186241
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
ID: 35186317
0
 
LVL 11

Accepted Solution

by:
Kusala Wijayasena earned 500 total points
ID: 35186587
Do like this

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

Open in new window


-Kusala
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

623 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