[英]Binding RadGridView with stored procedure's result in EntityFramework
I have stored the result of a stored procedure (in Entity Framework) in an IList
and then bind my grid with this IList
. 我已将存储过程的结果(在Entity Framework中)存储在
IList
,然后将网格与此IList
绑定。 When this result is null the grid hasn't got any columns but I need to show these columns in the grid. 当此结果为null时,网格没有任何列,但是我需要在网格中显示这些列。 Is there any way to solve this problem?
有什么办法解决这个问题?
This is my code: 这是我的代码:
IList list = new ArrayList();
try
{
var context = new SabzNegar01Entities1();
list = (from p in context.tbl_ReturnSalesFactor_D
let add = (p.MainNum * p.Fee)
let pureAdd = ((p.MainNum * p.Fee) - (p.MainNum * p.Discount)) + ((p.Tax + p.Charges) * p.MainNum)
let taxChange = (p.Tax + p.Charges) * p.MainNum
let discount = p.Discount * p.MainNum
where p.DocNo == inDocNo
select new { p.Row, p.StockCode, p.tbl_Stock.PDescription, p.Fee, p.MainNum, add, taxChange, discount, pureAdd }).ToList();
}
catch (Exception ex)
{
PMessageBox.Show(ex.Message, "Error in Reading ReturnSalesFactor_Details Data");
}
and binding: 和绑定:
radGridView_Product.DataSource = list ;
I would do this: 我会这样做:
define a C# class that matches the data that you're getting back from the stored procedure (eg SalesInfo
or whatever you want to call it) 定义一个与您从存储过程中获取的数据相匹配的C#类(例如
SalesInfo
或您要调用的任何数据)
then define your IList
to be a List<SalesInfo>
( please don't use the crappy old ArrayList
anymore!) 然后将您的
IList
定义为List<SalesInfo>
( 请不要再使用List<SalesInfo>
ArrayList
!)
when you call the stored procedure, but you get no values back, you just add a dummy SalesInfo
entry to your list being returned, that eg has no data found
as its description and everything else is empty/0.0 当您调用存储过程但没有返回任何值时,只需在返回的列表中添加一个虚拟
SalesInfo
条目即可,例如, no data found
描述作为no data found
,其他均为空/0.0
That way, your method will always return at least one element, and since that element is there, the gridview know it's columns and what to call them 这样,您的方法将始终返回至少一个元素,并且由于该元素在那里,GridView知道它是列以及如何调用它们
Update: 更新:
I would first define a class to hold all those properties you want to display in your gridview: 我首先定义一个类,以容纳要在gridview中显示的所有那些属性:
// just a data container to hold the information - call it whatever you like!
// also: with the datatypes, I am just *GUESSING* because you didn't exactly tell us
// what those values are - adapt as needed !
public class SalesInfo
{
public int Row { get; set; }
public string StockCode { get; set; }
public string Description { get; set; }
public decimal Fee { get; set; }
public decimal MainNum { get; set; }
public decimal Add { get; set; }
public decimal TaxChange { get; set; }
public decimal Discount { get; set; }
public decimal PureAdd { get; set; }
}
Next, define a method that goes and gets that data from the stored procedure - if not data is returned, add a dummy entry instead: 接下来,定义一个从存储过程中获取数据的方法-如果未返回数据,则添加一个虚拟条目:
// Define a method to return an IList of that data container class defined above
public IList<SalesInfo> GetSalesInfo()
{
// please, as of .NET 2.0 - use the List<T> and stop using ArrayList!
IList<SalesInfo> list = new List<SalesInfo>();
try
{
// put your context usage into using()..... blocks to ensure proper disposal
using (var context = new SabzNegar01Entities1())
{
// fetch the data, turn it into SalesInfo records
list = (from p in context.tbl_ReturnSalesFactor_D
where p.DocNo == inDocNo
select new SalesInfo
{
Row = p.Row,
StockCode = p.StockCode,
Description = p.tbl_Stock.PDescription,
Fee = p.Fee,
MainNum = p.MainNum,
Add = p.MainNum*p.Fee,
PureAdd = ((p.MainNum*p.Fee) - (p.MainNum*p.Discount)) + ((p.Tax + p.Charges)*p.MainNum),
Discount = p.Discount*p.MainNum,
TaxChange = (p.Tax + p.Charges)*p.MainNum
}).ToList();
}
// if you get back no data -> add a dummy entry
if (list.Count <= 0)
{
list.Add(new SalesInfo { Description = "(no data found)" });
}
}
catch (Exception ex)
{
PMessageBox.Show(ex.Message, "Error in Reading ReturnSalesFactor_Details Data");
}
// return the resulting list
return list;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.