2012-01-11 44 views
0

我正在设计一个C#表单程序,它使用列表框来显示来自SQL数据库的搜索结果。搜索功能可以工作,但现在我希望能够选择其中一个列表框行并将所选数据(来自SQL数据库)加载到新表单中。该计划追踪我们公司的客户。当用户键入某些条件时,列表框会被填充。我不熟悉SQL和C#表单设计,所以任何帮助都会很棒。我在列表框和搜索框下方留下了截图。表单列表框和SQL数据库

+0

“我在列表框和搜索框的下方留下了截图。”不,你没有。 – ean5533 2012-01-11 18:34:19

+0

什么样的形式? Silverlight的? – 2012-01-11 18:36:44

回答

0

带你愿意,你可以做到这一点(在后面的代码)的项目:

public void Test(object sender, EventArgs e) 
    { 
     ListBox list = (ListBox)sender; 
     var item = list.SelectedItem; 
    } 

一旦你的项目,你可以将它与一个事件或加载数据的方法。

0

你想调查的是你正在查看的各种控件的DataBinding属性。

+0

哇,非常快速的答案,谢谢。该程序是一个Windows C#表单程序,我没有使用银光。同样抱歉的截图,我没有足够的帖子在这个网站上传照片。我只有一些控件的DataBinding属性的一些经验,但不知道如何在这种情况下应用它。我有一个文本框和组合框,它允许用户在SQL数据库上进行搜索。搜索工作并在C#表单列表框中显示结果。 – user1143831 2012-01-11 18:53:54

0

我有一个快速搜索一个多月ago.This是我code.I希望它能帮助你:

这是我的存储过程的SQL中的全码:

USE [Khane] 
GO 
/****** Object: StoredProcedure [dbo].[QuickSerch] Script Date: 01/11/2012 22:24:56 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
-- ============================================= 
-- Author:  <Author,,Name> 
-- Create date: <Create Date,,> 
-- Description: <Description,,> 
-- ============================================= 
ALTER PROCEDURE [dbo].[QuickSerch] 
    @item nvarchar(300)=null 
AS 
BEGIN 
    select * from PersonsDataTbl 
where 
Name like '%'[email protected]+'%' or 
LastName like '%'[email protected]+'%' or 
FatherName like '%'[email protected]+'%' or 
NationalCode like '%'[email protected]+'%' or 
ShenasnameCode like '%'[email protected]+'%' or 
BirthDate like '%'[email protected]+'%' or 
State like '%'[email protected]+'%' or 
City like '%'[email protected]+'%' or 
Address like '%'[email protected]+'%' or 
PostalCode like '%'[email protected]+'%' or 
SportType like '%'[email protected]+'%' or 
SportStyle like '%'[email protected]+'%' or 
RegisterType like '%'[email protected]+'%' or 
Ghahremani like '%'[email protected]+'%' 
END 

,如果你不不知道存储过程,你可以搜索它。

,这是我的C#代码将数据发送到存储过程和获取数据的形式,它:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.SqlClient; 
using System.IO; 

namespace DL 
{ 

public class DLQuickSerch 
{ 
    List<Common.CommonPersonSerchResult> SerchResult = new List<Common.CommonPersonSerchResult>(); 

    public DLQuickSerch(string Item) 
    { 
     SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Khane;Integrated Security=True"); 

     SqlCommand command = new SqlCommand(); 
     command.Connection = connection; 

     command.CommandType = CommandType.StoredProcedure; 
     command.CommandText = "QuickSerch"; 

     SqlParameter item = new SqlParameter("item", SqlDbType.NVarChar, 300); 
     item.Value = Item; 
     command.Parameters.Add(item); 

     connection.Open(); 

     SqlDataReader reader = command.ExecuteReader(); 

     while (reader.Read()) 
     { 
      Common.CommonPersonSerchResult res = new Common.CommonPersonSerchResult(); 

      res.ID = (int)reader.GetValue(0); 
      res.FirstName = reader.GetValue(1).ToString(); 
      res.LastName = reader.GetValue(2).ToString(); 
      res.FatherName = reader.GetValue(3).ToString(); 
      res.NationalCode = (int)reader.GetValue(4); 
      res.ShenasnameCode = (int)reader.GetValue(5); 
      res.BirthDate = reader.GetValue(6).ToString(); 
      res.State = reader.GetValue(7).ToString(); 
      res.City = reader.GetValue(8).ToString(); 
      res.Address = reader.GetValue(9).ToString(); 
      res.PostalCode = reader.GetValue(10).ToString(); 
      res.SportType = reader.GetValue(11).ToString(); 
      res.SportStyle = reader.GetValue(12).ToString(); 
      res.RegisterType = reader.GetValue(13).ToString(); 
      res.Ghahremani = reader.GetValue(14).ToString(); 
      SerchResult.Add(res); 

     } 

     connection.Close(); 

    } 

    public List<Common.CommonPersonSerchResult> GetQuickSerchResult() 
    { 
     return SerchResult; 
    } 
} 
} 

,你应该用你的数据库的数据

改变SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Khane;Integrated Security=True");,这是我的显示代码在ListView数据:

private void QuickSearch(string item) 
{ 
    DL.DLQuickSerch qc = new DL.DLQuickSerch(item); 
    List<Common.CommonPersonSerchResult> reslt = qc.GetQuickSerchResult(); 
    FillListView(reslt); 
} 

private void FillListView(List<Common.CommonPersonSerchResult> list) 
    { 
     SerchResultList.Items.Clear(); 
     foreach (Common.CommonPersonSerchResult c in list) 
     { 
      ListViewItem item = new ListViewItem(); 
      item.Text = c.ID.ToString(); 
      item.SubItems.Add(c.FirstName); 
      item.SubItems.Add(c.LastName); 
      item.SubItems.Add(c.FatherName); 
      item.SubItems.Add(c.NationalCode.ToString()); 
      item.SubItems.Add(c.ShenasnameCode.ToString()); 
      item.SubItems.Add(c.BirthDate); 
      item.SubItems.Add(c.State); 
      item.SubItems.Add(c.City); 
      item.SubItems.Add(c.PostalCode.ToString()); 
      item.SubItems.Add(c.SportType); 
      item.SubItems.Add(c.SportStyle); 
      item.SubItems.Add(c.RegisterType); 
      item.SubItems.Add(c.Ghahremani); 

      item.Tag = c; 

      SerchResultList.Items.Add(item); 

     } 
    } 

和我Common.CommonPersonSerchResult只是一个类的属性。

要小心,这是我的代码,当你需要使用它在你的项目

用于显示在新的形式的数据,你可以保存从DB在列表框的标签获得了数据,你应该改变它,后得到新形式的构造函数中的数据,并在您使用它的新form.It是如此easy.To作出新的形式,你可以用列表框的选择更改事件的工作是这样的:

private void SerchResultList_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (SerchResultList.SelectedItems.Count != 0) 
     { 
      Form2 = new Form2(SerchResultList.SelectedItems[0].Tag); 
     } 
    } 

这个代码是为一个ListView,我认为ListBox中只有一个SelectedItem,你的方法将如下所示:

private void SerchResultList_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (SerchResultList.SelectedItem != null) 
     { 
      Form2 = new Form2(SerchResultList.SelectedItem); 
     } 
    } 

,你应该给列表框中的数据作为一个列表框的项目,你应该重写你的数据CALSS的的ToString()方法来显示在ListBox中你需要的数据。