Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sort/Select with DataTable

Posted on 2011-03-22
2
Medium Priority
?
331 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?

I was advised to write :
 GridView1.DataSource = GetDataTable(@"c:\fans.csv").Select("Name = 'Xname'");
But I get "Field or property with name 'Name' was not found on the selected data source



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = GetDataTable(@"c:\fans.csv").Select("Name = 'Xname'");
            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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 35192799
I would try like this:

 
DataTable dt = GetDataTable(@"c:\fans.csv");
DataView dv = dt.DefaultView;
dv.Rowfilter = "Name = 'Xname'";
dv.Sort = "Age"; // as example
GridView1.DataSource = dv;

Open in new window


for the rest, I see nothing wrong, actually
0
 

Author Closing Comment

by:portalvale
ID: 35192978
Awesome... u r an expert!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to insert an Iframe into 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 : Open Page or Post…
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…
Suggested Courses

636 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