简体   繁体   中英

unbound row in datagridview in c#

I have

Class A
{
private string mName;
private int mId;
private int mType;
private int mqty;
/*
property to view variables.
*/
}

BindingList<A> listofA =  new BindingList<A>();

"listofA" is bound to to datagridview control.

dataGridView1.DataSource=listofA;

which is displayed as.

------------------
|Name|Id|Type|Qty|
------------------
|CatA| 1|   0|  5|
|CatB| 2|   0|  6|
|DogA| 3|   1|  7|
|DogB| 4|   1|  8|

I want an unbound row to display.

------------------
|Name|Id|Type|Qty|
------------------
|CatA| 1|   0|  5|
|CatB| 2|   0|  6|
|Cats|__|   0| 11| <------sum of qty 
|DogA| 3|   1|  7|
|DogB| 4|   1|  8| 
|Dogs|__|   1| 15| <------sum of qty

I was thinking of adding a Bool in "class A" and it would act as an item if false and as sum if true.

Is there a better way to do this.

What I would do is code in the RowDataBound event :

1) compare data from previous row (ex:animal type). If different then you know you've reached the end of a category, if the same, you should sum-up the different values you need to sum up

2a) type was the same, you did the sumup, there's nothing more to do

2b) type was different, you create a new row with the sum-up values and reset the sum variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM