Question: my winForms app has a tab control which consists of two tabs (tab1 & tab2). In tab2 data is fetched in a datagridview fron a database(Product infomations).
In tab1, I've a combobox [sales analyse]which makes a user to select an option.
I now want to get access to tab2 from tab1 on cb selection, displaying me a regional sales information from the data in tab2 datagrid.
Is it possible? I don't really know wher to start

tab1 image 
![enter image description here](https://i.stack.imgur.com/vNxkR.png)

tab2

![enter image description here](https://i.stack.imgur.com/T3tAk.png)
Expectation:

if the combobox in tab1 is selected, it should then look through the datagridview in tab2 where the (regions) North, East, West ect are and then sum the sale 13, sales 14 .. and display in the textBoxes respectively.

Answer: 
A possible `data.table` solution

You could define an helper function

```
tempfunc <- function(x) c(sum(x * data$weight), sum(data$weight[x > 0]))

```

Then do either

```
library(data.table)
setDT(data)[, lapply(.SD, tempfunc), .SDcols = -c("id", "weight")]
#          a       b        c
# 1: 995.579 598.310 172.8193
# 2: 120.319 119.662 138.0090

```

Or

```
library(dplyr)
setDT(data) %>% summarise_each(funs(tempfunc), -c(id, weight))
##          a       b        c
## 1: 995.579 598.310 172.8193
## 2: 120.319 119.662 138.0090

```
The following code should solve your question:

```
my.names <- names(data)[names(data) %in% letters]

res <- lapply(my.names, function(x){
  c(sum(data[[x]]*data[["weight"]]), sum(data[["weight"]][data[[x]]>0]))
})

names(res) <- my.names

```

or directly to `data.frame`:

```
do.call("rbind", lapply(my.names, function(letter){
  data.frame(letter, "sum1_name" = sum(data[[letter]]*data[["weight"]]), 
             "sum2_name" = sum(data[["weight"]][data[[letter]]>0]))
}))

# letter sum1_name sum2_name
# 1      a  995.5790   120.319
# 2      b  598.3100   119.662
# 3      c  172.8193   138.009

```