在表单上显示搜索结果 [英] display search result on a form

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

问题描述

我正在寻找有关在我的应用程序中搜索带有姓氏"的客户信息时如何显示结果的帮助
我想在 列表视图 (客户端ID,姓氏,其他名称)中显示具有多个结果的新表单,并允许用户选择他们要查找的表单

类中的代码

I''m looking for help on how to display results when searching for clients informations with their "surname" on my application
I want to display a new form with multiple results in a List View (clientid, surname, othername) and allow the user to select the one they are looking for

codes in class

public bool searchpersonDetails(string personname)
	    {
           if (surname == string.Empty) 
                {
                    Fom1 frm = new Fom1(personname);
		    frm.Show();
		    personname = surname.ToString();

	        }

            SqlCommand cmd = new SqlCommand();
	    string sqlQuery = null;

            sqlQuery = "select client_id, surname, othername from tblspersonaldetails where surname like ''%" + personname + "%'';";

            cmd.Connection = conn;
            cmd.CommandText = sqlQuery;
	    cmd.CommandType = System.Data.CommandType.Text;

            SqlDataReader dr = null;
	    dr = cmd.ExecuteReader();
	if (dr.Read()) 
            {
                
		client_id = dr["client_id"].ToString();
                surname = dr["surname"].ToString();
                othername = dr["othername"].ToString();

                return true;
            }

            else
            {
                return false;
            }

        }

    }
}




按钮后面的代码





codes behind button click


private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                getid getidentity = new getid();

                if (getidentity.searchpersonDetails(txtSurname.Text))
                {

                    var
                    _with9 = this;

                    _with9.txtSurname.Text = getidentity.Sname.ToString();
                }

                else
                {
                    MessageBox.Show("Record not found");
                }
            }


            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                // Close data reader object and database connection


                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }

        }
    }
}




表格后面的代码




codes behind form

public partial class Fom1 : Form
    {

            SqlConnection conn = new SqlConnection();

            public Fom1(string personname)
            {
                InitializeComponent();

                // Add any initialization after the InitializeComponent() call.
                SqlCommand cmd = new SqlCommand();


                string sqlQuery = null;
                //this criteria As String = personName & "%"
                sqlQuery = "select * from tblspersonaldetails where surname like ''" + personname + "%" + "'' order by surname,othername asc";
                cmd.Connection = conn;
                cmd.CommandText = sqlQuery;
                cmd.CommandType = System.Data.CommandType.Text;
                SqlDataReader dr = null;
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    ListViewItem lst = default(ListViewItem);

                    lst = this.listView1.Items.Add(dr["client_id"].ToString());
                    lst.SubItems.Add(dr["surname"].ToString());
                    lst.SubItems.Add(dr["othername"].ToString());
                }
                cmd.Dispose();

            }




但是仍然无法显示在窗体上,请帮助我




BUT IT IS STILL NOT DISPLAYING ON THE FORM PLease HELP ME OUT

推荐答案

这并不困难,但是很难给您代码,因为我们不这样做不知道你怎么做.但是过程本身非常简单:

1)从数据库中检索信息.
2)将其打包为合适的结构-List< T>或DataTable(如果您稍后使用,则可以很容易地与上面的(1)组合)
3)创建您的新表单,并将您的结构添加到默认构造函数中.因此,例如,如果您确实使用了DataTable,则默认构造函数将获得一个参数-DataTable.
4)添加一个公共属性(仅用于获取方法),该属性将返回所选信息-id,DataRow或您交给构造函数的List元素
5)将信息添加到网格中,并提供确定"和取消"按钮.

完成!
要使用它,请创建新表单的实例,并向其传递信息.调用表单实例的ShowDialog方法,并检查OK返回.如果得到它,则从属性中获取选定的信息.
This isn''t difficult, but it is hard to give you code because we don''t know how you do things. But the process itself is pretty simple:

1) Retrieve the info from the database.
2) Package it into a suitable structure - a List<T>, or a DataTable (if you use the later, it can be combined with (1) above very easily)
3) Create your new form and add your structure to teh default constructor. So if for example you do use a DataTable, the default constructor gets a single parameter - the DataTable.
4) Add a public property (a getter only) which returns the selected info - id, DataRow, or element of List that you handed in the constructor
5) Add the info to your grid and supply an OK and Cancel button.

Done!
To use it, create an instance of the new form, passing it the info. Call the ShowDialog method of the form instance, and check for OK return. If you get it, get the selected info from the property.


根据您的最新注释,似乎没有代码实际将结果放入表单中.您是否应该有以下内容:
...
Based on your latest comment, there seems to be no code that''s actually putting the results in the form. Should you have something like:
...
TextBoxSurName.Text = dr["surname"].ToString();
...


使用类似
的代码
use code like
public datatable search(string sirname) //in your class
{
   datatable dt=new datatable();
   new sqldataadaptor("query",con).fill(dt);
   return dt;
}


在btn上单击


on btn click

datatable dt=classobject.search(txtsirname); //input by user
 datagrid.datasource=dt; //bind datagrid here


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

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