Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

c# - How to connect Tab Controls

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...