K C
K C

Reputation: 219

Creating row header in asp.net gridview

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.

Upvotes: 0

Views: 95

Answers (1)

balanza
balanza

Reputation: 1079

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

Upvotes: 3

Related Questions