1

我需要帮助,我只是想将一个简单的组合框绑定到包含此数据的 sql 数据库中的表:

EmployeesTbl:
-------------
EmpID    EmpName   JobID   HireDate     Deleted 
1        Tom       1       12/4/2005    false
2        steve     1       5/5/2005     false
3        Maria     2       2/8/2010     false
<Window x:Class="TestGlassButton.Window4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window4" Height="300" Width="300">
       <Grid>
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="54,213,0,0" 
                  Name="cmbCapsNme" VerticalAlignment="Top" Width="132" 
                  ItemsSource="{Binding}"
                  DisplayMemberPath="EmpArbName"
                  SelectedValuePath="EmpID"/>
       </Grid>
 </Window>
namespace TestGlassButton
{

public partial class Window4 : Window
{
    public DataSet SelAllCaps()
    {
        SqlConnection SqlCon = new SqlConnection("Data Source=.;Initial Catalog=Rest;User ID=m*201m;Password=12345");
        // captains names & ID   
        SqlCommand SqlCmdAllCaps = new SqlCommand();
        SqlCmdAllCaps.Connection = SqlCon;
        SqlCmdAllCaps.CommandType = CommandType.Text;
        SqlCmdAllCaps.CommandText = "select EmpID ,EmpArbName from dbo.EmployeesTbl where JobID = 3 order by EmpArbName";
        SqlCon.Open();
        SqlDataAdapter dapAllCaps = new SqlDataAdapter();
        dapAllCaps.SelectCommand = SqlCmdAllCaps;
        DataSet dsAllEmpsInfo = new DataSet();
        dapAllCaps.Fill(dsAllEmpsInfo, "EmployeesTbl");
        SqlCon.Close();
        return dsAllEmpsInfo;
    }

    private void BindCaps()
    {

        DataSet ds = SelAllCaps();
        List<CapsClass> capList = new List<CapsClass>();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            capList.Add(new CapsClass(Convert.ToInt32(ds.Tables[0].Rows[i]["EmpID"]),
                ds.Tables[0].Rows[i]["EmpArbName"].ToString()));
        }
        ds = null;
        cmbCapsNme.ItemsSource = capList;
        cmbCapsNme.DisplayMemberPath = "EmpArbName";
        cmbCapsNme.SelectedValuePath = "EmpID";
        cmbCapsNme.SelectedIndex = -1;
    }

    public Window4()
    {
        InitializeComponent();
        BindCaps();
     }
}
class CapsClass
{
    Int32 _capID=-1;
    string _capNme;

    public CapsClass(Int32 CapID, String CapNme)
    {
        this._capID = CapID;
        this._capNme = CapNme;
    }

    public Int32 CapID
    {
        get { return _capID; }
        set { _capID = value; }
    }

    public string CapNme
    {
        get { return _capNme; }
        set { _capNme = value; }
    }
}
}

但是当我执行我的项目时,我的组合框中没有出现数据,它只显示没有。的行。有什么错误和我的代码,有什么想法吗?

4

1 回答 1

0

您的组合框具有 DisplayMemberPath="EmpArbName" 和 SelectedValuePath="EmpID" 但您的 CapClass 没有具有此名称的属性。

我将尝试将该属性更改为 CapID 和 CapNme,或直接绑定到数据表。

cmbCapsNme.ItemsSource = ds.Tables[0];
于 2012-04-28T14:23:25.990 回答