0

I have this one strange requirement in asp.net grid view. Is it possible to have column header and row header at the same time as shown in the image below?

enter image description here

If yes I do have one more questions

1.) I want to automatically calculate failure percentage in Failure% row based on Failed Quantity and Lot Quantity as we do in excel.

Help is deeply appreciated, Thanks in advance.

1 Answer 1

3

I guess you have a collection, let's call it origCollection which contains data in some way. What I usually do is to transform origCollection into another, let's call it newCollection, which has the form you want. You shall need this class to map single row:

Class myRow {
    public string title {get; set;} //Failure %, Failed Quantity, ...
    public string test1{get; set;}
    public string test2{get; set;}
    public string test3{get; set;}
    public string test4{get; set;}
    public string test5{get; set;}
}

Then, before databind, you can create newCollection this way:

//create newCollection
var newCollection = new List<myRow>();

//for each row you wnt to put in newCollection:
newCollection.Add(new myRow(){
   title = "Failure %",
   test1 = // calculate value from origCollection,
   test2 = // calculate value from origCollection,
   test3 = // calculate value from origCollection,
   test4 = // calculate value from origCollection,
   test5 = // calculate value from origCollection
} );

The way you compose newCollection from origCollection depends deeply from your data structure, anyway you can wrap all the process in a function, let's call it transformCollection(). Then data bind is easy:

var newcoll = transformCollection(origCollection);
this.myGrid.DataSource = newcoll;
this.myGrid.Databind();

That's the way I usually do, I hope this help

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @balanza. It did help but the solution tend to grow in length if number of rows increase which would raise questions on features of Object orientation. Is not there a grid view property or any other control which could satisfy the requirement as I have?
@user2470766 For post-processing cell values, you can always use GridView.RowDataBound event. But for handling the structure of the data table, I can only suggest to use an intermediate transformation collection, as above. What did you mean with "the solution tend to grow in length if number of rows increase which would raise questions on features of Object orientation"?
In the above solution for each row we have to put in a new collection. If my data has hundred's of rows then for each row data we have to write the newCollection.Add(new myRow() part of the code again.
can't you manage it with a for-loop? How is data binding right now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.