Frage

I have stored the result of a stored procedure (in Entity Framework) in an IList and then bind my grid with this IList. When this result is null the grid hasn't got any columns but I need to show these columns in the grid. 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 ;
War es hilfreich?

Lösung

I would do this:

  • define a C# class that matches the data that you're getting back from the stored procedure (e.g. SalesInfo or whatever you want to call it)

  • then define your IList to be a List<SalesInfo> (please don't use the crappy old ArrayList anymore!)

  • 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 e.g. has no data found as its description and everything else is empty/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

Update:

I would first define a class to hold all those properties you want to display in your 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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top