portalvale
asked on
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(val ue))
{
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;
}
I was advised to write :
GridView1.DataSource = GetDataTable(@"c:\fans.csv
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
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(val
{
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER