Combobox选择的值不起作用



我的代码有问题,基本上我正在尝试一个组合框,当选择团队时,它将显示与该团队连接到该团队的球员。问题是我对此有很多错误

这是用团队填充组合框的代码

   String sqlTeam = @"select * from Team";
        daTeam = new SqlDataAdapter(sqlTeam, connStr);
        SqlCommandBuilder cmdBTeam = new SqlCommandBuilder(daTeam);
        daTeam.FillSchema(dsCIF, SchemaType.Source, "Team");
        DataTable dtTeam = new DataTable();
        daTeam.Fill(dsCIF, "Team");
        daTeam.Fill(dtTeam);
        cmbTeam1.DataSource = dtTeam;
        cmbTeam1.ValueMember = "TeamID";
        cmbTeam1.DisplayMember = "TeamName";
        cmbTeam2.DataSource = dtTeam;
        cmbTeam2.ValueMember = "TeamID";
        cmbTeam2.DisplayMember = "TeamName";

当团队在组合框中更改时,它将通过连接到该团队的球员对列表框进行重新填充,这是ListBoxes

的代码
  sqlPlayers3 = @" Select PlayerID, PlayerForename, PlayerSurname, PlayerAge, PlayerStatus, (Playerforename +' '+Playersurname) as FullName from Player where TeamID  = @cmb";
        SqlCommand cmd = new SqlCommand();
        cmd.Parameters.AddWithValue("@cmb", int.Parse(cmbTeam1.SelectedValue.ToString()));
        String connStr5 = Properties.Resources.cString;
        SqlDataAdapter daPlayer4 = new SqlDataAdapter(sqlPlayers3, Properties.Resources.cString);
        DataTable dtPlayers3 = new DataTable();
        daPlayer4.Fill(dtPlayers3);
        lbTeam1Goal1.DataSource = dtPlayers3;
        lbTeam1Goal1.ValueMember = "PlayerID";
        lbTeam1Goal1.DisplayMember = "FullName";

        lbTeam1Bookings1.DataSource = dtPlayers3;
        lbTeam1Bookings1.ValueMember = "PlayerID";
        lbTeam1Bookings1.DisplayMember = "FullName";

问题是,加载表单时,我会继续遇到此错误

cmd.Parameters.AddWithValue("@cmb", int.Parse(cmbTeam1.SelectedValue.ToString()));

System.FormatException: 'Input string was not in correct format.'

我已经采取了许多不同的方式,但是该代码似乎不起作用。我正在使用SQL数据库,并连接到Visual Studio C#。我有什么想念还是做错了?

您的程序中有两个问题

您看到的'Input string was not in correct format.'的错误是由于ComboBox.SelectedIndexChanged()事件处理程序绑定数据后就触发的。在定义ValueMemberDisplayMember之前,您正在绑定数据。这意味着您的SelectedValue属性将不知道哪个列保存了选定值。

在绑定数据之前,请始终定义您的ValueMemberDisplayMember。这样:

 //...
 lbTeam1Goal1.ValueMember = "PlayerID";
 lbTeam1Goal1.DisplayMember = "FullName";
 lbTeam1Goal1.DataSource = dtPlayers3; //databinding goes last
 lbTeam1Bookings1.ValueMember = "PlayerID";
 lbTeam1Bookings1.DisplayMember = "FullName";
 lbTeam1Bookings1.DataSource = dtPlayers3;

接下来,您没有使用参数,因为您的DataAdapter使用查询字符串而不是命令对象。尝试以下操作:

 sqlPlayers3 = @" Select PlayerID, PlayerForename, PlayerSurname, PlayerAge, PlayerStatus, 
     (Playerforename +' '+Playersurname) as FullName from Player 
     where TeamID  = @cmb";
 SqlCommand cmd = new SqlCommand(sqlPlayers3); //SQL goes in the command constructor
 cmd.Parameters.AddWithValue("@cmb", int.Parse(cmbTeam1.SelectedValue.ToString()));
 String connStr5 = Properties.Resources.cString;
 //SqlDataAdapter daPlayer4 = new SqlDataAdapter(sqlPlayers3, Properties.Resources.cString); //not this way
 SqlDataAdapter daPlayer4 = new SqlDataAdapter(cmd, Properties.Resources.cString); //this will work better
 DataTable dtPlayers3 = new DataTable();
 daPlayer4.Fill(dtPlayers3);
 //...

在更改代码之前,您可能会发现在cmd.Parameters.AddWithValue的行上放一个断点。您会看到SelectedIndexChanged甚至要发射3次:1(设置DataSource、2(设置ValueMember和3(设置DisplayMember时。更改后,设置DataSource时将发射一次。

相关内容

  • 没有找到相关文章

最新更新