Bind to datagridview
-
I feel like a dummy asking this but dammed if I can get it right. I have 2 datatables and I need to do a left join so using the MSDN sample this gets me an AsEnumerable of datarows. I want to bind this to a DGV. So far the most useful has been to use AsDataView(). However this will not allow a join in the query. So what am I doing wrong.
var Result = from Q in dtQ.AsEnumerable() join A in dtMap.AsEnumerable() on Convert.ToString(Q.Field("IssuerID")) equals A.Field("IDValue") select new { IssuerID = Q.Field("IssuerID"), AttrID = A.Field("IDValue") }; dgResult.DataSource = Result;
Never underestimate the power of human stupidity RAH
-
I feel like a dummy asking this but dammed if I can get it right. I have 2 datatables and I need to do a left join so using the MSDN sample this gets me an AsEnumerable of datarows. I want to bind this to a DGV. So far the most useful has been to use AsDataView(). However this will not allow a join in the query. So what am I doing wrong.
var Result = from Q in dtQ.AsEnumerable() join A in dtMap.AsEnumerable() on Convert.ToString(Q.Field("IssuerID")) equals A.Field("IDValue") select new { IssuerID = Q.Field("IssuerID"), AttrID = A.Field("IDValue") }; dgResult.DataSource = Result;
Never underestimate the power of human stupidity RAH
Doing a left join is a little odd in linq... try this.
var Result =
from Q in dtQ.AsEnumerable()
join A in dtMap.AsEnumerable() on Convert.ToString(Q.Field("IssuerID"))
equals A.Field("IDValue") into group
from g in group.DefaultIfEmpty()
select new
{
IssuerID = Q.Field("IssuerID"),
AttrID = g.Field("IDValue")
};
dgResult.DataSource = Result;
dgResult.DateBind();I think this may do it. Cant tell 100% without trying this myself. But this is how I have done left joins in linq.
-
Doing a left join is a little odd in linq... try this.
var Result =
from Q in dtQ.AsEnumerable()
join A in dtMap.AsEnumerable() on Convert.ToString(Q.Field("IssuerID"))
equals A.Field("IDValue") into group
from g in group.DefaultIfEmpty()
select new
{
IssuerID = Q.Field("IssuerID"),
AttrID = g.Field("IDValue")
};
dgResult.DataSource = Result;
dgResult.DateBind();I think this may do it. Cant tell 100% without trying this myself. But this is how I have done left joins in linq.
Thanks Ian, first blush has a number of errors with typing, I'll chase them down in the morning. I thought databind was only for the asp GridView, I'm using winforms datagridview.
Never underestimate the power of human stupidity RAH
-
Thanks Ian, first blush has a number of errors with typing, I'll chase them down in the morning. I thought databind was only for the asp GridView, I'm using winforms datagridview.
Never underestimate the power of human stupidity RAH
My bad I thought it was asp.net. Correct you dont need that for winforms.