如何连接选项卡控件 [英] How to connect Tab Controls

查看:28
本文介绍了如何连接选项卡控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 winForms 应用程序有一个选项卡控件,它由两个选项卡(tab1 和 tab2)组成.在 tab2 中,数据是从数据库(产品信息)的 datagridview 中获取的.在 tab1 中,我有一个组合框 [销售分析],它使用户可以选择一个选项.我现在想在 cb 选择上从 tab1 访问 tab2,向我显示来自 tab2 数据网格中数据的区域销售信息.是否可以?我真的不知道从哪里开始

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 图片

tab2

期望:

如果选择了 tab1 中的组合框,则它应该查看 tab2 中的 datagridview,其中(区域)North, East, West ect 所在的位置,然后将销售额 13、销售额 14 相加并分别显示在文本框中.

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.

推荐答案

由于您的控件都位于一个 Form 中,因此它们的方法都可以相互引用,无需任何额外帮助.

As your controls all sit in one Form their methods can all reference each other without any additional help.

所以你可以写在ComboBox cbAnalyse

cbAnalyse_SelectedIndexChanged(object sender, EventArgs e) 
{ 
   if (cbAnalyse.SelectedItem.ToStringndex == "Sales Analysis"
   {
        someTextbox1.Text = ColumnSum(yourDataGridView, someColumn1) + "$";
        someTextbox2.Text = ColumnSum(yourDataGridView, someColumn2) + "$";
   }

这使用了一个小的辅助函数,它汇总了 DataGridView 中一列的所有值:

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

当人们需要引用位于同一表单但位于第二、第三等表单中的控件时,他们经常会遇到问题.或者当它们是 Usercontrol 的一部分时,这是一个用于保存控件的客户容器.

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.

在这两种情况下,这些控件默认都是其他表单或 UserObject 的私有成员.

In both cases those controls are by default private members of the other Forms or of the UserObject.

在这些情况下,需要为它们创建某种公共访问器,通常是通过属性.而在 Forms 的情况下,还需要提供对其他表单的引用,通常在打开它们时存储.

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.

在这种情况下,第 2 种形式通常也需要对第 1 种形式进行反向引用;这通常在构造函数中传入.

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

更新: 由于您在获取中间总和时似乎也遇到问题并且需要允许在 Tab 2 的行中重复区域,因此您还想使用一个函数来计算where子句:

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

这篇关于如何连接选项卡控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆