如何在c#中的单独表单上显示搜索结果? [英] how to Display search reasults on a separate form in c# ?

查看:87
本文介绍了如何在c#中的单独表单上显示搜索结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的伙计们,

我正在使用MS Access数据库在C#.net中开发应用程序.

我想要以下选项:

当我单击搜索按钮时,成员的所有可用信息都应在另一个带有打印按钮的表单上显示,U可以从中将其打印出来.

这是代码:

Dear Guys,

I''m developing an application in C#.net with a MS Access database.

I want the following option:

When I click the search button all available info of a member should display on another form with a print button from where U can take print out.

Here is the code:

private void button1_Click(object sender, EventArgs e)
   {
      string sql = "Select * from login where (Name='" + textBox1.Text + "')";

            OleDbDataAdapter adapter = new OleDbDataAdapter(sql, myCon);

            DataTable login = new DataTable();

            adapter.Fill(login);

            if (login.Rows.Count > 0)
            {
                textBox1.Text = login.Rows[0]["Name"].ToString();
                MessageBox.Show("Record found");                
            }
            else
            {
                textBox1.Text = "";
                MessageBox.Show("No Record Found");
            }
}


任何人都可以指导实现我的目标并完善上述代码.
谢谢,


Can anybody guide to achieve my target and refine the above code.
Thanks,

推荐答案

您应该将结果存储在一个对象(可能是您的DataTable)中,然后将该对象传递给这种形式(从我回答一个问题的答案中复制出来的答案)回来时):
这是原始问题:如何将值一"表单传递给Windows表单中的另一表单 [
You should store the results in an Object (probably your DataTable) and pass that Object to another form like this (answer copied from a question I answered a while back):
Here is the original question: How to pass the Value one form to another form in windows form[^]

To pass Objects between Forms you can follow one of these approaches:
One is using a Property on one of your Forms.
MyForm frm = new MyForm();
// It is possible to set MyValue at any point in your code, as long as you have a reference to an instance of MyForm.
frm.MyValue = "some value";
frm.Show();


另一种是将其传递给构造函数.


Another is passing it to the constructor.

public partial class MyForm : Form
{
   private String _myValue;
   public Form1(String myValue)
   {
      InitializeComponent();
      _myValue = myValue;
      // Possibly use myValue here.
   }
   // Do stuff with _myValue here.
}

用法如下:

Usage would look like this:

MyForm frm = new MyForm("some value");
frm.Show();


另一种方法是使用方法...


Another approach could be to use a Method...

public partial class MyForm : Form
{
   private String _myValue;
   public void SetValue(String myValue)
   {
      _myValue = myValue;
     // Possibly do stuff with myValue here.
   }
   // Or use _myValue here.
}

用法:

MyForm frm = new MyForm();
frm.Show();
// Once again, you can use this anywhere in your code, as long as you have a reference to the instance of MyForm.
frm.SetValue("some value");



因此,现在您将数据保存在另一个表单中,只需在此处处理,编辑等,然后在其原始来源的表单中对其进行更新.

希望对您有所帮助:)



So now you got your data in another Form, all you need to do is handle it there, edit it etc. and update it in the Form it originally came from.

Hope it helps :)


这篇关于如何在c#中的单独表单上显示搜索结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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