Reputation: 712
I am trying to show data from DataTable into GridView using a LINQ query but I am not able to understand that why this code is not working. Actually GridView shows RowError HasError message. I am totally confused.
Here's my code
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = (DataTable)Session["userTable"];
string compare = Request.QueryString["id"].ToString();
var data = from x in table.AsEnumerable()
where x.Field<string>("Userid") == compare
select x;
GridView1.DataSource = data;
GridView1.DataBind();
}
Upvotes: 0
Views: 481
Reputation: 4069
Your query returns IEnumerable<DataRow>
to convert it to Datatble
use CopyToDataTable()
extension. MSDN
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = (DataTable)Session["userTable"];
string compare = Request.QueryString["id"].ToString();
IEnumerable<DataRow> data = from x in table.AsEnumerable()
where x.Field<string>("Userid") == compare
select x;
DataTable boundTable = data.CopyToDataTable<DataRow>();
GridView1.DataSource = boundTable;
GridView1.DataBind();
}
Upvotes: 2
Reputation: 403
Try this,
var data = from x in table.AsEnumerable()
where x.Field<string>("UserId").ToUpper().ToString().Equals(compare.ToUpper().ToString())
select x;
DataTable boundTable = data.AsDataView().ToTable();
return boundTable;
Upvotes: 1