根据pp_shy 的做法。写成一下代码。点击搜索按钮时就没有反应了。pp_shy 帮我看看FORM1 //打开搜索界面 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Dim frm As New FormCustomerSearch() frm.ShowDialog() Dim strWhre As String = frm.strSql Dim conn As New SqlConnection("server=HJSQL01\DEVELOP;database=scheduler_test;uid=sa;pwd=sa;") Dim adt As New SqlDataAdapter(strWhre, conn) Dim dataset As New DataSet adt.Fill(dataset, "jobs") DataGridView1.DataSource = dataset.Tables(0) frm.Dispose() End Subform2 Public strSql As String //点击搜索按钮 Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click Dim a1 As String = Me.TextBox1.Text.Trim().ToString() Dim a2 As String = Me.TextBox2.Text.Trim().ToString() Dim a3 As String = Me.TextBox3.Text.Trim().ToString() Dim a4 As String = Me.TextBox4.Text.Trim().ToString() Dim a5 As String = Me.TextBox5.Text.Trim().ToString() Dim a6 As String = Me.TextBox6.Text.Trim().ToString() Dim a7 As String = Me.TextBox7.Text.Trim().ToString() Dim a8 As String = Me.TextBox8.Text.Trim().ToString() Dim a9 As String = Me.TextBox9.Text.Trim().ToString() Dim a10 As String = Me.TextBox10.Text.Trim().ToString() Dim strSql As New StringBuilder("select * from I_CustomOrder where 1=1 ") If a1 <> "" And a2 <> "" Then strSql.Append(" and WRFAC between '" + a1 + "' and '" + a2 + "' ") End If If a3 <> "" And a4 <> "" Then strSql.Append(" and WRLOC between '" + a3 + "' and '" + a4 + "'") End If If a5 <> "" And a6 <> "" Then strSql.Append(" and WRPROD between '" + a5 + "' and '" + a6 + "'") End If If a7 <> "" And a8 <> "" Then strSql.Append(" and WRTOOL between '" + a7 + "' and '" + a8 + "'") End If If a9 <> "" And a10 <> "" Then strSql.Append(" and WRRDTE between '" + a9 + "' and '" + a10 + "'") End If strSql.Append("order by WRTOOL,WRRDTE desc ")
' Form_customer_order.Instance(Me) 'Form_customer_order.ShowDialog() Me.Close() End Sub
根据pp_shy 的做法。写成一下代码。点击搜索按钮时就没有反应了。pp_shy 帮我看看FORM1 //打开搜索界面 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Dim frm As New FormCustomerSearch() frm.ShowDialog() Dim strWhre As String = frm.strSql Dim conn As New SqlConnection("server=HJSQL01\DEVELOP;database=scheduler_test;uid=sa;pwd=sa;") Dim adt As New SqlDataAdapter(strWhre, conn) Dim dataset As New DataSet adt.Fill(dataset, "jobs") DataGridView1.DataSource = dataset.Tables(0) frm.Dispose() End Subform2 Public strSql As String //点击搜索按钮 Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click Dim a1 As String = Me.TextBox1.Text.Trim().ToString() Dim a2 As String = Me.TextBox2.Text.Trim().ToString() Dim a3 As String = Me.TextBox3.Text.Trim().ToString() Dim a4 As String = Me.TextBox4.Text.Trim().ToString() Dim a5 As String = Me.TextBox5.Text.Trim().ToString() Dim a6 As String = Me.TextBox6.Text.Trim().ToString() Dim a7 As String = Me.TextBox7.Text.Trim().ToString() Dim a8 As String = Me.TextBox8.Text.Trim().ToString() Dim a9 As String = Me.TextBox9.Text.Trim().ToString() Dim a10 As String = Me.TextBox10.Text.Trim().ToString() Dim strSql As New StringBuilder("select * from I_CustomOrder where 1=1 ") If a1 <> "" And a2 <> "" Then strSql.Append(" and WRFAC between '" + a1 + "' and '" + a2 + "' ") End If If a3 <> "" And a4 <> "" Then strSql.Append(" and WRLOC between '" + a3 + "' and '" + a4 + "'") End If If a5 <> "" And a6 <> "" Then strSql.Append(" and WRPROD between '" + a5 + "' and '" + a6 + "'") End If If a7 <> "" And a8 <> "" Then strSql.Append(" and WRTOOL between '" + a7 + "' and '" + a8 + "'") End If If a9 <> "" And a10 <> "" Then strSql.Append(" and WRRDTE between '" + a9 + "' and '" + a10 + "'") End If strSql.Append("order by WRTOOL,WRRDTE desc ")
' Form_customer_order.Instance(Me) 'Form_customer_order.ShowDialog() Me.Close() End Sub
DataTable dtb = opDbFun.SearchWord(keyWord, databaseName, procName1);//查询函数
Test tst = new Test();
tst.dt = dtb;
tst.Show();
this.Hide();
Form2:
public DataTable dt;
private void Test_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = dt;
}
{
Form2 frm = new Form2();
frm.ShowDialog();
string strWhre = frm.strWhere;//strWhere是Form2中生成的Where条件组合语句
..........................................
执行查询条件,刷新datagridview过程
frm.Dispose();
}Form2:public string strWhere; //定义一个全局变量,用于存放Form2中的生成的Where查询条件private void button1_Click(object sender, System.EventArgs e)//查询按钮事件
{
............................
//生成where查询条件过程
strWhere = "最终生成的查询条件";
this.close();//关闭Form2窗体
}
Form1里面有一个文本框TextBox1,在文本框中可以输入你要查询的条件,Form1里还有一个按钮Button1
Form2里面只有一个DataGridView,命名为DataGridView1下面在Button1的单击事件中编写代码:
private void Button1_Click(object sender, System.EventArgs e)
{
string strSql=this.TextBox1.Text;
Form2 newForm=new Form2(strSql);//Form2的构造函数,用于传值
this.hide();
newForm.Show();
}然后到Form2中编写Form2的构造函数:
private string strSql;
public string Form2(string strSql):this()
{
this.strSql=strsql;
}
最后编写Form2的Load事件:private void Form2_Load(object sender, System.EventArgs e)
{
//先建立数据库连接sqlcon,在此就不说了
SqlDataAdapter sqladp=new SqlDataAdapter(strSql,sqlcon);
DataSet ds=new DataSet();
sqladp.Fill(ds,"FillDataGridView1");
this.DataGridView1.DataSource=ds.table["FillDataGridView"];//这一句不知道有没有写对,因为我没有在编译器里面去写
this.DataGridView1.DataBind();
}运行正常的话,应该就能出结果了
难道你不是通过主界面(Form1)上的一个按钮事件打开一个(Form2)的高级检索界面吗?
Form1中的按钮事件是用来打开Form2窗体并等待Form2返回查询条件,然后刷新DataGridView
Form2中的按钮事件是用来处理生成最终的查询条件字符串的
//打开搜索界面
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim frm As New FormCustomerSearch()
frm.ShowDialog()
Dim strWhre As String = frm.strSql Dim conn As New SqlConnection("server=HJSQL01\DEVELOP;database=scheduler_test;uid=sa;pwd=sa;")
Dim adt As New SqlDataAdapter(strWhre, conn)
Dim dataset As New DataSet
adt.Fill(dataset, "jobs")
DataGridView1.DataSource = dataset.Tables(0)
frm.Dispose() End Subform2
Public strSql As String
//点击搜索按钮
Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click Dim a1 As String = Me.TextBox1.Text.Trim().ToString()
Dim a2 As String = Me.TextBox2.Text.Trim().ToString()
Dim a3 As String = Me.TextBox3.Text.Trim().ToString()
Dim a4 As String = Me.TextBox4.Text.Trim().ToString()
Dim a5 As String = Me.TextBox5.Text.Trim().ToString()
Dim a6 As String = Me.TextBox6.Text.Trim().ToString()
Dim a7 As String = Me.TextBox7.Text.Trim().ToString()
Dim a8 As String = Me.TextBox8.Text.Trim().ToString()
Dim a9 As String = Me.TextBox9.Text.Trim().ToString()
Dim a10 As String = Me.TextBox10.Text.Trim().ToString() Dim strSql As New StringBuilder("select * from I_CustomOrder where 1=1 ") If a1 <> "" And a2 <> "" Then strSql.Append(" and WRFAC between '" + a1 + "' and '" + a2 + "' ")
End If If a3 <> "" And a4 <> "" Then
strSql.Append(" and WRLOC between '" + a3 + "' and '" + a4 + "'")
End If
If a5 <> "" And a6 <> "" Then
strSql.Append(" and WRPROD between '" + a5 + "' and '" + a6 + "'")
End If
If a7 <> "" And a8 <> "" Then
strSql.Append(" and WRTOOL between '" + a7 + "' and '" + a8 + "'")
End If
If a9 <> "" And a10 <> "" Then
strSql.Append(" and WRRDTE between '" + a9 + "' and '" + a10 + "'")
End If strSql.Append("order by WRTOOL,WRRDTE desc ")
' Form_customer_order.Instance(Me)
'Form_customer_order.ShowDialog()
Me.Close() End Sub
//打开搜索界面
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim frm As New FormCustomerSearch()
frm.ShowDialog()
Dim strWhre As String = frm.strSql Dim conn As New SqlConnection("server=HJSQL01\DEVELOP;database=scheduler_test;uid=sa;pwd=sa;")
Dim adt As New SqlDataAdapter(strWhre, conn)
Dim dataset As New DataSet
adt.Fill(dataset, "jobs")
DataGridView1.DataSource = dataset.Tables(0)
frm.Dispose() End Subform2
Public strSql As String
//点击搜索按钮
Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles search.Click Dim a1 As String = Me.TextBox1.Text.Trim().ToString()
Dim a2 As String = Me.TextBox2.Text.Trim().ToString()
Dim a3 As String = Me.TextBox3.Text.Trim().ToString()
Dim a4 As String = Me.TextBox4.Text.Trim().ToString()
Dim a5 As String = Me.TextBox5.Text.Trim().ToString()
Dim a6 As String = Me.TextBox6.Text.Trim().ToString()
Dim a7 As String = Me.TextBox7.Text.Trim().ToString()
Dim a8 As String = Me.TextBox8.Text.Trim().ToString()
Dim a9 As String = Me.TextBox9.Text.Trim().ToString()
Dim a10 As String = Me.TextBox10.Text.Trim().ToString() Dim strSql As New StringBuilder("select * from I_CustomOrder where 1=1 ") If a1 <> "" And a2 <> "" Then strSql.Append(" and WRFAC between '" + a1 + "' and '" + a2 + "' ")
End If If a3 <> "" And a4 <> "" Then
strSql.Append(" and WRLOC between '" + a3 + "' and '" + a4 + "'")
End If
If a5 <> "" And a6 <> "" Then
strSql.Append(" and WRPROD between '" + a5 + "' and '" + a6 + "'")
End If
If a7 <> "" And a8 <> "" Then
strSql.Append(" and WRTOOL between '" + a7 + "' and '" + a8 + "'")
End If
If a9 <> "" And a10 <> "" Then
strSql.Append(" and WRRDTE between '" + a9 + "' and '" + a10 + "'")
End If strSql.Append("order by WRTOOL,WRRDTE desc ")
' Form_customer_order.Instance(Me)
'Form_customer_order.ShowDialog()
Me.Close() End Sub