学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
发新话题
打印

怎样在查询窗口中控制过滤另一窗口中的数据记录

怎样在查询窗口中控制过滤另一窗口中的数据记录

我用C#2005做了个程序,其中有一个查询功能,首先打开一个窗口Form1,里面的datagridview控件显示出所有记录,然后我点击菜单中的查询命令,打开查询窗口可以选择查询条件,点击查询窗口的确定按钮后Form1窗口执行"Form名.bindingSource.Filter = sql语句"命令来过滤记录。现在问题是查询窗口中建立的Form1的实例和打开Form1建立的不是同一实例,所以执行以上命令后Form1窗口中的记录无变化,怎样才能在查询窗口中引用到原来打开Form1窗口时建立的实例?

TOP

有人知道吗

TOP

我现在是这样做的
主窗口Form1类如下(部分相关代码):
public partial class Form_lirun : Form
{
    public Form_lirun()//构造函数
    {
        InitializeComponent();
    }

    public void filter(string p_str_filter)//对本窗口显示的数据进行过滤
    {
        lirunbindingSource.Filter=p_str_filter;
    }
}

查询窗口类代码:
public partial class Form_query : Form
{
    private Form_lirun frMain;
    public Form_query(Form_lirun fr)
    {
        InitializeComponent();
        this.frMain=fr;
    }

    private void button6_Click(object sender, EventArgs e)
    {
        this.frMain.filter(p_str_sql);//调用Form1窗口的自定义方法
    }
}
但是在执行到this.frMain.filter(p_str_sql)这句时系统报错,说没有创建实例,该怎样修改啊

TOP

把 datagridview 控传过去

把第一个页面的 datagridview 传到第二个页面~!

TOP

代码 第一个页面的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            string conn = "server=.; database=pubs; uid=sa; pwd=sa;";
            SqlConnection con = new SqlConnection(conn);

            try
            {
                con.Open();
                string strsql = "select * from jobs";
                SqlDataAdapter ada = new SqlDataAdapter(strsql,con);
                DataSet objset = new DataSet();
                ada.Fill(objset);
                this.dataGridView1.DataSource = objset.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fm = new Form2(this.dataGridView1);
            fm.ShowDialog();
        }
    }
}

TOP

代码 第二个页面的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication4
{
    public partial class Form2 : Form
    {
        DataGridView dg;
        public Form2(DataGridView dg1)
        {
            dg = dg1;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int id = int.Parse(this.textBox1.Text);


            string conn = "server=.; database=pubs; uid=sa; pwd=sa;";
            SqlConnection con = new SqlConnection(conn);

            try
            {
                con.Open();
                string strsql = "select * from jobs where job_id='"+id+"'";
                SqlDataAdapter ada = new SqlDataAdapter(strsql, con);
                DataSet objset = new DataSet();
                ada.Fill(objset);
                dg.DataSource = objset.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }

        }
    }
}

TOP

可以用代理吗

TOP

什么意思了~!

TOP

谢谢楼上的,我已经参照你的方法将第一个窗口中的bindingsource控件传到第二个窗口中了,已经好用了
非常感谢

TOP

发新话题