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
tab2
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.
As your controls all sit in one Form their methods can all reference each other without any additional help.
So you can write in the SelectedIndexChanged
of the ComboBox cbAnalyse
cbAnalyse_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbAnalyse.SelectedItem.ToStringndex == "Sales Analysis"
{
someTextbox1.Text = ColumnSum(yourDataGridView, someColumn1) + "$";
someTextbox2.Text = ColumnSum(yourDataGridView, someColumn2) + "$";
}
This uses a small helper function, which sums up all values from one column in a DataGridView:
decimal ColumnSum(DataGridView dgv, int columnIndex)
{
decimal sum = 0m;
for (int row = 0; row < DGV.Rows.Count; row++)
if (DGV[columnIndex, row].Value != null) sum += Convert.ToDecimal(DGV[1, row].Value);
return sum;
}
Folks often run into problems when they need to refer to controls that are not sitting in either the same Form but in a 2nd, 3rd etc Form. Or when they are part of a Usercontrol
, which is a custome container for holding controls.
In both cases those controls are by default private members of the other Forms or of the UserObject.
In these cases one needs to create some kind of public accessor to them, usually by a Property. And in the case of Forms, one also need to provide a reference to the other forms, often stored when opening them.
In this case, the 2nd Form often also needs a back-refrence to the 1st Form; this is often pass in in the constructor.
But in your case none of these complications matter. All you need is the patience to wire up all those TextBoxes ;-)
Update: As you also seem to have a problem getting the intermediate sums and need to allow for repeating regions in the rows of Tab 2, you also want to use a function that will calculate with a where clause:
decimal ColumnSumWhere(DataGridView dgv, int columnIndex, string region)
{
decimal sum = 0m;
for (int row = 0; row < DGV.Rows.Count; row++)
if (DGV[columnIndex, row].Value != null) &&
(DGV[regionColumn, row].Value.ToString() == region)
sum += Convert.ToDecimal(DGV[1, row].Value);
return sum;
}
where clause
to sum up the attributes i need. I think your sample sums the whole column right? Back to my DVG in the column sales13, the values for north are 2x 800. So the txtBox for north shd display 1600 - James
If I got it right, whenever you change the value in sales analyze combo, the tab page containing data grid should should be activate. You can set the selected index of the tab control to the the data grid tab, and it should work
this.tabControl1.SelectedIndex = 1;//Index of data grid tab
combobox
is selected, it should then look through the datagridview
in tab2 where the North, East, West ect is and sum the sale 13, sales 14 .. and display in the textBoxes respectively. I have updated my question - James
combobox
for the selction. - Jamescb selection
? An event? The selectionChanged of the ComboBox? Or a CommandButton?? Please edit your question to be precise and complete!! - TaWcb selection
the selectionChanged of the ComboBox. That means if the combobox is selected, it should then look through the datagridview in tab2 where the North, East, West ect is and sum the sale 13, sales 14 .. and display in the textBoxes respectively. - James