• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Sort/Select with DataTable

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
portalvale
Asked:
portalvale
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
portalvaleAuthor Commented:
Awesome... u r an expert!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now