Solved

How to filter/sort with DataTable

Posted on 2011-03-21
4
272 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
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

770 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