现在问题我已经找到了,但是不知道怎么解决,先看看代码把//子窗体 form1
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        cnnStr cnn = new cnnStr();
        DataSet ds = new DataSet(); 
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            ds.Clear();
            cnn.cnstr();
            OleDbDataAdapter dt = new OleDbDataAdapter("Select top 10 id,userid,username,truename,sex from usermember where sex='" + Form2.sex + "'", cnn.cn);
            dt.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Function.ExportDataGridViewToExcel(dataGridView1);
        }
    }
}//父窗体
namespace WindowsApplication1
{
    public partial class Form2 : Form
    {
        public static string sex="男";
        Form1 f1 = new Form1();
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            tabControl1_SelectedIndexChanged(sender, e);
        }        private void button1_Click(object sender, EventArgs e)
        {
            Function.ExportDataGridViewToExcel(f1.dataGridView1);
        }        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            f1.MdiParent = this;
            f1.FormBorderStyle = FormBorderStyle.None;
            f1.Dock = DockStyle.Fill;
            
            
            int tcl=tabControl1.SelectedIndex;
            switch (tcl) 
            {
                case 0:
                    {
                        this.tabPage1.Controls.Clear();
                        sex = "男";
                        this.tabPage1.Controls.Add(f1);
                        break;
                    }
                case 1:
                    {
                        this.tabPage2.Controls.Clear();
                        sex = "女";
                        this.tabPage2.Controls.Add(f1);
                        break;
                    }
            }
            f1.Show();
        }
    }
}//类
namespace WindowsApplication1
{
    class cnnStr
    {
        public OleDbConnection cn;
        public void cnstr()
        {
            String conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.mdb";
            cn = new OleDbConnection(conString);
        }
    }
}
//自定义函数FUNCTION
namespace WindowsApplication1
{
    class Function
    {
        public static void ExportDataGridViewToExcel(DataGridView dataGridview1)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "导出Excel文件到";
            saveFileDialog.FileName = "导出";
            //saveFileDialog.ShowDialog();
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = saveFileDialog.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
                string str = "";
                try
                {
                    //写标题  
                    for (int i = 0; i < dataGridview1.ColumnCount; i++)
                    {
                        if (i > 0)
                        {
                            str += "\t";
                        }
                        str += dataGridview1.Columns[i].HeaderText;
                    }                    sw.WriteLine(str);
                    //写内容 
                    for (int j = 0; j < dataGridview1.Rows.Count - 1; j++)
                    {
                        string tempStr = "";
                        for (int k = 0; k < dataGridview1.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                tempStr += "\t";
                            }
                            tempStr += dataGridview1.Rows[j].Cells[k].Value.ToString();
                        }
                        sw.WriteLine(tempStr);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }    }
}描述:Form1窗体中有个选项卡控件,选项卡有2个卡,“男”“女”,要求能单击每个选项卡查出数据库里面的相应数据Form2窗体为主窗体,程序启动时通过代码将Form1窗体添加进来。在Form2上面有个导出dataGridView里面查询出来的数据现在问题是:
在子窗体里面能导出Excel并有数据,但是在主窗体里面导出来后没有数据我自己检查出来的原因是把private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)里面的“Form1 f1 = new Form1();”给去掉,但是去掉后单击选项卡查询数据就无效了大家帮我分析分析把。该怎么解决谢谢拉