-1

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

tab2

enter image description here 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.


  • To access anything you just need a reference to it. If your TabPages are basically static you can add the references in any way you like. But as they are all in the same form anyway(?) they already see each other. So there is nothing to do at all..! - This is not true if the TabPages contain Usercontrols.Do they? If so, please update qour question! - TaW
  • @TaW. An example will be great for a start. The only user control is the combobox for the selction. - James
  • The only user control is the combobox Probably not. See here for the difference between CustomControls and UserControls! If it is a CustomControls (aka subclass) there is no problem. If it is a UserControl after all, you would have to expose its fields in some way, best via Properties.. - TaW
  • What is cb selection ? An event? The selectionChanged of the ComboBox? Or a CommandButton?? Please edit your question to be precise and complete!! - TaW
  • @TaW. Thnx. I do understand u now. It's a UserControl. cb 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

2 답변


0

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;
}


  • Thanks TaW. I will check on your suggestion and give you a feedback if I succeed or not. I hope I will succed. Schönen Tag noch ;-) - James
  • Did you resolve your problem? Alles klar? - TaW
  • Hi TaW. Unfortunately I couldn't. The problem is that I think I need a 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
  • in the column sales13, the values for north are 2x 800. So the txtBox for north shd display 1600 Um, no. Do you mean 800+200=1000? Is it correct to have reapeating rows in the Tab2? (This was not there when I wrote my answer, I believe..) If so, What about the rows with the '?' in the Tab1? Not also repeating, I hope..?! - TaW
  • See if my my edit helps! - TaW

0

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


  • No tab2 shouldn't be activated. 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. I have updated my question - James
  • @James: In that case, you must have some datasource of your grid for example data table. Just find the record from the datasource where "Region" is equal to combo selected value, and once you have got the record, find the respective values from "Sales13, 14 and 15" respectively. - Yogi

Linked


Latest