Gridview table data manage in different view add sub heads - c#

I want to manage the data of gridview in different format like add sub heads
code of this data :
var data = (from r in data
join texp in totalexplist on r.SancPropId equals texp.AllSanchAmtId into list2
from l2 in list2.DefaultIfEmpty()
select new
{
Id = r.Id,
FYear = r.FYear,
SancPropId = r.SancPropId,
ExpSubHeadId = r.ExpSubHeadId,
ExpSubHeadName = r.ExpSubHeadName,
ProposedAmount = r.ProposedAmount,
SanctionedAmount = r.SanctionedAmount,
MonthYear = l2 == null ? "" : l2.MnthYear,
Expenditure = l2 == null ? 0 : l2.Exp,
}).ToList();
grd.DataSource = data;
grd.DataBind();
But i want to show this data in below given form
i have try this code but stuck in add to subheads
var monthwise = (from t in data
group t by new { t.MonthYear}
into grp
select new
{
grp.Key.MonthYear,
Data = grp.ToList()
}).ToList();
var allColumns = monthwise.SelectMany(d => d.Data.Select(s => s.ExpSubHeadName)).Distinct().ToList();
var datatable = new DataTable();
datatable.Columns.Add(new DataColumn("Month"));
allColumns.ForEach(c => datatable.Columns.Add(new DataColumn(c)));
Is anyone know how can i manage grid in this form ?

Related

GridView Only populating 1 result

I'm currently working to add Data to a GridView. The data comes from 2 tables that are on different databases. Currently I am able to populate the first entry, but it does not populate past that. here is the code:
void FillOrder(int inv)
{
var _ord = new OrdersContext();
var _pro = new ProductContext();
var qryOrder = (from o in _ord.OrderDetails
where o.InvNumberId == inv
select new
{
o.ProductID,
o.Quantity
}).ToList();
foreach (var order in qryOrder)
{
int prodID = order.ProductID;
int itemCount = qryOrder.Count;
var qryProducts = (from p in _pro.Products
where p.ProductID == prodID
select new
{
p.ProductID,
p.ProductName
}).ToList();
var results = (from t in qryOrder
join s in qryProducts
on t.ProductID equals prodID
select new
{
t.ProductID,
t.Quantity,
s.ProductName
}).ToList();
OrderItemList.DataSource = results;
OrderItemList.DataBind();
}
}
Can anyone help as to why it's only populating the first entry?
If the number of products involved is relatively small, (and since this query seems to be relate to one invoice, I would think that is true), then you can probably use something like the code below.
This is removing the loop, but the contains method will probably generate a SQL statement something like select ProductID, ProductName from products where productID in (,,,,,,) so may fail if the number of parameters is extremely large.
var _ord = new OrdersContext();
var _pro = new ProductContext();
var qryOrder = (from o in _ord.OrderDetails
where o.InvNumberId == inv
select new
{
o.ProductID,
o.Quantity
}).ToList();
// Get the productIDs
var productIDS = qryOrder.Select(o=>o.ProductID).Distinct().ToList();
// Get the details of the products used.
var qryProducts = (from p in _pro.Products
where productIDS.Contains(p.ProductID)
select new
{
p.ProductID,
p.ProductName
}).ToList();
// Combine the two in memory lists
var results = (from t in qryOrder
join s in qryProducts
on t.ProductID equals s.ProductID
select new
{
t.ProductID,
t.Quantity,
s.ProductName
}).ToList();
OrderItemList.DataSource = results;
OrderItemList.DataBind();

Date selection comparison with Date_Time data column using Linq query in ASP.NET Gridview

I need my Linq query to compare Date_Time (has only date in data not date_time, datatype - nvarchar) column with date selection of Date1.Value (string) but without showing any error my Gridview doesn't get populated. If I remove the Date_Time where condition, the page runs well. I would be really thankful if someone could help me out with this.
Regards,
Tejas
private void populateData()
{
// here code for populate data
using (TrialEntities13 dc = new TrialEntities13())
{
var v = (from p in dc.Vehicle_Andon
where (p.Line_Name == DropDownList1.SelectedValue
&& p.Loss_Type == 9 && p.Date_Time == Date1.Value)
join s in dc.LOSS_TYPE_MASTER on p.Loss_Description equals s.Loss_Description
select new
{
p,
s.Loss_Description,
});
List<Vehicle_Andon> allP = new List<Vehicle_Andon>();
foreach (var i in v)
{
Vehicle_Andon p = new Vehicle_Andon();
p = i.p;
p.Loss_Description = i.Loss_Description;
allP.Add(p);
}
GridView1.DataSource = allP;
GridView1.DataBind();
}
}

Join List Data from two separate Data Contexts

I have been searching for the solution to this problem and this is what I have so far:
var ProductInfo = (from p in twd.Products
orderby p.PC
where p.DELMARK == active
select p).AsEnumerable();
var BuyersData =
(from x in db.MinimumProductInfo
where x != null
orderby x.ItemCode, x.Region
let pnote =
(from pn in db.ProductNotes
where pn != null
where x.MinimumProductInfoID == pn.MinimumProductInfoID
&& pn.NoteTypeFlag == "p"
orderby pn.NoteDate descending
select pn).FirstOrDefault()
let cnote =
(from c in db.ProductNotes
where c != null
where x.MinimumProductInfoID == c.MinimumProductInfoID
&& c.NoteTypeFlag == "c"
orderby c.NoteDate descending
select c).FirstOrDefault()
let product =
(from p in ProductInfo
where x.ItemCode == p.PC
select p).FirstOrDefault()
select new ProductInfoWithNoteList
{
MinimumProductInfoID = x.MinimumProductInfoID,
ItemCode = x.ItemCode,
EquivCode = x.EquivCode,
Description = product.PDESC,
MinimumOnHandQuantity = x.MinimumOnHandQuantity,
MaximumOHandQuantity = x.MaximumOHandQuantity,
MinimumOrderQuantity = x.MinimumOrderQuantity,
LeadTimeInWeeks = x.LeadTimeInWeeks,
Region = x.Region,
Comment = cnote.ItemNote,
PermanentNote = pnote.ItemNote
}).ToArray();
It looks correct but I am getting an error,
'The specified LINQ expression contains references to queries that are
associated with different contexts.'
What this code is supposed to do is pull out all the active product codes from the first table using the twd datacontext then use data from that database in the db.MinimumProductInfo table. The reason they have 2 separate data contexts are they are completely different databases, the first is our ERP and the second is one that we are building in house.
What am I missing? I know it is possible to do this by separating the two datacontexts then adding them together because I have seen it done with single instances but I cannot find how to do it with list data.
Instead of this:
let product =
(from p in ProductInfo
where x.ItemCode == p.PC
select p).FirstOrDefault()
select new ProductInfoWithNoteList
{
MinimumProductInfoID = x.MinimumProductInfoID,
ItemCode = x.ItemCode,
EquivCode = x.EquivCode,
Description = product.PDESC,
MinimumOnHandQuantity = x.MinimumOnHandQuantity,
MaximumOHandQuantity = x.MaximumOHandQuantity,
MinimumOrderQuantity = x.MinimumOrderQuantity,
LeadTimeInWeeks = x.LeadTimeInWeeks,
Region = x.Region,
Comment = cnote.ItemNote,
PermanentNote = pnote.ItemNote
}).ToArray();
Try this by removing the let product clause and not filling the properties associated with ProductInfo because we will do that afterwards (See I have commented out the Description property):
select new ProductInfoWithNoteList
{
MinimumProductInfoID = x.MinimumProductInfoID,
ItemCode = x.ItemCode,
EquivCode = x.EquivCode,
//Description = product.PDESC,
MinimumOnHandQuantity = x.MinimumOnHandQuantity,
MaximumOHandQuantity = x.MaximumOHandQuantity,
MinimumOrderQuantity = x.MinimumOrderQuantity,
LeadTimeInWeeks = x.LeadTimeInWeeks,
Region = x.Region,
Comment = cnote.ItemNote,
PermanentNote = pnote.ItemNote
}).ToArray();
Now that you have your BuyersData and ProductInfo in memory, set the Description property or all the items in BuyersData:
foreach(var thisBuyerData in BuyersData)
{
var thisPi = ProductInfo.SingleOrDefault(x => x.PC == thisBuyerData.ItemCode);
thisBuyerData.Description = thisPi?.PDESC;
}

Error on using GroupBy in Linq

~ error says image seen below i don't know why is it? is there another way to pick up the data without the error? i'm using distinct or group by.
var query = (from i in SFC.TransacRRrecords where i.POPRCTNM == p
group i by new {
i.VendorID,
i.VENDNAME,
i.SUBTOTAL,
i.PymtrMID,
i.TRUCKNO,
i.REMARKS,
i.VPNum,
i.VPDate,
i.AmountInWords
} into grp
select new
{
Vcode = grp.Key.VendorID,
Vdesc = grp.Key.VENDNAME,
Vsubtotal = grp.Key.SUBTOTAL,
Vterms = grp.Key.PymtrMID,
Vtruckno = grp.Key.TRUCKNO,
Vremarks = grp.Key.REMARKS,
Vvpno = grp.Key.VPNum,
Vvpdate = grp.Key.VPDate,
Vamntword = grp.Key.AmountInWords
}).FirstOrDefault();
ListViewItem List = new ListViewItem(query.Vcode);
List.SubItems.Add(query.Vdesc);
List.SubItems.Add(string.Format("{0:n2}", query.Vsubtotal));
List.SubItems.Add(query.Vterms);
List.SubItems.Add(query.Vtruckno);
List.SubItems.Add(query.Vremarks);
List.SubItems.Add(query.Vvpno);
List.SubItems.Add(query.Vvpdate);
List.SubItems.Add(query.Vamntword);
deletedRRHeaderList.Items.AddRange(new ListViewItem[] { List });

how convert DataTable to List<String> in C#

I am using C# Linq now I am converting DataTable to List
and I am getting stuck...
give me right direction thanks..
private void treeview1_Expanded(object sender, RoutedEventArgs e)
{
coa = new List<string>();
//coa = (List<string>)Application.Current.Properties["CoAFull"];
HMDAC.Hmclientdb db = new HMDAC.Hmclientdb(HMBL.Helper.GetDBPath());
var data = (from a in db.CoA
where a.ParentId == 0 && a.Asset == true
select new { a.Asset, a.Category, a.CoAName, a.Hide, a.Recurring, a.TaxApplicable });
DataTable dtTable = new DataTable();
dtTable.Columns.Add("Asset", typeof(bool));
dtTable.Columns.Add("Category", typeof(string));
dtTable.Columns.Add("CoAName", typeof(string));
dtTable.Columns.Add("Hide", typeof(bool));
dtTable.Columns.Add("Recurring", typeof(bool));
dtTable.Columns.Add("TaxApplicable", typeof(bool));
if (data.Count() > 0)
{
foreach (var item in data)
{
DataRow dr = dtTable.NewRow();
dr["Asset"] = item.Asset;
dr["Category"] = item.Category;
dr["CoAName"] = item.CoAName;
dr["Hide"] = item.Hide;
dr["Recurring"] = item.Recurring;
dr["TaxApplicable"] = item.TaxApplicable;
dtTable.Rows.Add(dr);
}
}
coa = dtTable;
}
It seems that you already have a strongly typed list. Why converting this to a weakly typed DataTable and then back to a list?????
var data =
from a in db.CoA
where a.ParentId == 0 && a.Asset == true
select new
{
a.Asset,
a.Category,
a.CoAName,
a.Hide,
a.Recurring,
a.TaxApplicable
};
var list = data.ToList();
If you want to be able to use this list outside the scope of the method, define a type that will hold the different properties and in your select statement use this type instead of the anonymous type like:
var data =
from a in db.CoA
where a.ParentId == 0 && a.Asset == true
select new MyType
{
Asset = a.Asset,
Category = a.Category,
CoAName = a.CoAName,
Hide = a.Hide,
Recurring = a.Recurring,
TaxApplicable = a.TaxApplicable
};
List<MyType> list = data.ToList();
You don't need the data table according to the code you have displayed:
var data = (from a in db.CoA
where a.ParentId == 0 && a.Asset == true
select new { a.Asset.ToString() + a.Category.ToString()
+ a.CoAName.ToString()... }).ToList();
If you really want to convert your datatable to a 1D list, you can do it like this
foreach (DataRow row in dtTable.Rows)
{
foreach (DataColumn col in dtTable.Columns)
{
coa.Add(row[col]);
}
}
As you are using Select new in you linq query It will find object. What you can do is
var data = (from a in db.CoA
where a.ParentId == 0 && a.Asset == true
select new { a.Asset, a.Category, a.CoAName, a.Hide, a.Recurring, a.TaxApplicable });
this is your query and you select multiple columns in your query. So you can't convert your data to a single List of string. What you can do is concatenate all the column in a single string and then add them in a list of string.
To do that modify your query like 'CK' said
var data = (from a in db.CoA
where a.ParentId == 0 && a.Asset == true
select new { a.Asset.ToString() + a.Category.ToString()
+ a.CoAName.ToString()... }).ToList();
And then do
List<string> name = new List<string>(data.ToList());

Categories

Resources