gridview asp.net doubts

RAVI 936 Reputation points
2022-03-17T13:01:54.407+00:00

Hello

I have one gridview from that im extracting each 20 quantity from the gridview1 data into gridview2 data

this is my c# code

private void BindGridView()
{
    int SrNoCounter = Convert.ToInt32("1");
    List<ItemAllocation> gridSrc = new List<ItemAllocation>();
    double remQty = 0;
    double itemRemQty = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
        Label WORKORDERSIZE = (Label)row.FindControl("L2");
        Label WORKORDERQty = (Label)row.FindControl("L3");             

        double qtyCounter = 0;
        double totalQty = Convert.ToDouble(WORKORDERQty.Text);
        double eachQty = Convert.ToDouble(TextBox3.Text.Trim());
        remQty = totalQty;
        eachQty -= itemRemQty;
        while (qtyCounter <= totalQty && remQty >= eachQty)
        {
            ItemAllocation srcRec = new ItemAllocation();
            srcRec.Name = WORKORDERSIZE.Text;
            srcRec.Article = WORKORDERQty.Text;               
            srcRec.BoxNo = SrNoCounter;
            srcRec.EachQty = eachQty;
            gridSrc.Add(srcRec);

            qtyCounter += eachQty;
            remQty -= eachQty;
            SrNoCounter++;
            if (itemRemQty > 0)
            {
                eachQty = Convert.ToDouble(TextBox1.Text.Trim());
                itemRemQty = 0;
            }
        }
        if (remQty > 0)
        {
            itemRemQty = remQty;
            ItemAllocation srcRec = new ItemAllocation();
            srcRec.Name = WORKORDERSIZE.Text;
            srcRec.Article = WORKORDERQty.Text;                
            srcRec.BoxNo = SrNoCounter;
            srcRec.EachQty = remQty;
            gridSrc.Add(srcRec);
        }
    }
    if (null != gridSrc &&
            gridSrc.Count > 0)
    {
        GridView3.DataSource = gridSrc;
        GridView3.DataBind();
    }
}


public class Item
{
    private string name;
    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            this.name = value;
        }
    }

    private double zname;
    public double Qty
    {
        get
        {
            return this.zname;
        }
        set
        {
            this.zname = value;
        }
    }



}


public class ItemAllocation
{
    private int ABC;
    public int BoxNo
    {
        get
        {
            return this.ABC;
        }
        set
        {
            this.ABC = value;
        }
    }

    private string DEF;
    public string Name
    {
        get
        {
            return this.DEF;
        }
        set
        {
            this.DEF = value;
        }
    }
    private double GHI;
    public double EachQty
    {
        get
        {
            return this.GHI;
        }
        set
        {
            this.GHI = value;
        }
    }

    private string JKL;
    public string Article
    {
        get
        {
            return this.JKL;
        }
        set
        {
            this.JKL = value;
        }
    }
}

output like this
Box / Roll No Each Box Wise Pieces Work Order Size Work Order Quantity
1 20 7 30
2 10 7 30
2 10 8 20
3 10 8 20

but i want like thi

Box / Roll No Each Box Wise Pieces Work Order Size Work Order Quantity
1 20 7 30
2 10 7 30
3 20 8 20

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,313 questions
{count} votes

Accepted answer
  1. Michael Taylor 49,246 Reputation points
    2022-03-17T15:35:28.517+00:00

    Firstly I'd like to point out that you're not really using GridView the way it is meant to be used. GridView is for showing a list of data. You shouldn't be extracting data out of it by enumerating rows in the UI. The UI displays data from your data source. You edit the data source and the UI updates. If changes are made in the UI then it automatically updates the data source and you just go from there. The only possible case I can see where you'd use the GridView like you are is if you displayed a grid of items and all fields are editable but that isn't really what GridView is designed for either. Nevertheless let's fix the problem you have with your existing code.

    You said you have this

       1 20 7 30  
       2 10 7 30  
       2 10 8 20  
       3 10 8 20  
    

    But want this. Note that it is unclear how these numeric values actually map to your ItemAllocation type that you're creating.

       1 20 7 30  
       2 10 7 30  
       3 20 8 20  
    

    You didn't tell us the rules you are following to get there. I can see you're grouping by the Box but after that it is unclear. The first and last rows are just from your original data set so we can infer nothing here. But box 2 has two rows and it seems like you just selected the first one. To get this aggregated data you'll need to use LINQ to fetch the data you want for the second UI. Perhaps something like this.

       //Represents the data you said you had but completely guessing at values  
       var gridSrc = new[]  
       {  
           new ItemAllocation() { BoxNo = 1, Name = "20", EachQty = 7, Article = "30" },  
           new ItemAllocation() { BoxNo = 2, Name = "10", EachQty = 7, Article = "30" },  
           new ItemAllocation() { BoxNo = 2, Name = "10", EachQty = 8, Article = "20" },  
           new ItemAllocation() { BoxNo = 3, Name = "10", EachQty = 8, Article = "20" },  
       };  
         
       //You didn't specify the rules so assuming group by BoxNo and then grab remaining values from first one  
       var groupedItems = from i in gridSrc  
                           group i by i.BoxNo into g  
                           select new ItemAllocation()  
                           {  
                               BoxNo = g.Key,  
                               Name = g.First().Name,  
                               EachQty = g.First().EachQty,  
                               Article = g.First().Article  
                           };  
         
       var finalItems = groupedItems.ToArray();  
    

    This is using grouping but there are other approaches as well including using Distinct and a custom comparer or storing the values into a dictionary by BoxNo and selecting just the first item.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. RAVI 936 Reputation points
    2022-03-17T13:03:34.853+00:00

    gridview1 data
    184181-image.png

    184162-image.png

    0 comments No comments