简体   繁体   English

C#代码用参数化的组合框过滤组合框

[英]c# code filtering a combobox with a paramatized combobox

I'm writing a program where I add a math-class to sql but use combo boxes for some of the foreign keys so that they can only choose from certain items in a reference table( for example the class names come from a different table with all the class names in. But for some reason the classnameid does not give correct id's when I use the combobox.selectedvalue function. it keeps on giving weird -14 and -25 as id's which is wrong. please help. here is my code 我正在编写一个程序,向sql中添加数学类,但对某些外键使用组合框,以便它们只能从参考表中的某些项目中进行选择(例如,类名来自于具有所有类名都在其中。但是由于某种原因,当我使用combobox.selectedvalue函数时,classnameid并没有给出正确的ID。它会继续给-14和-25作为ID,这是错误的。请帮忙。这是我的代码

    private void btnAddClass_Click(object sender, EventArgs e)
    {
        int iclassroomID = Convert.ToInt16(cmbClassRoomName.SelectedValue);
        int iclassTypeID = Convert.ToInt16(cmbClassType.SelectedValue);
        int iHours = Convert.ToInt16(cmbHours.SelectedItem);
        int iMins = Convert.ToInt16(cmbMinutes.SelectedItem);
        string sClassLength = txtLength.Text;
        DateTime dtClassdate;
        dtClassdate = dateTimePicker1.Value;
        DateTime myClassDateandTime = dtClassdate.Date.AddHours(iHours).AddMinutes(iMins);
        txtOutput.Text = Convert.ToString(iclassroomID);



        int selectedyear = this.dateTimePicker1.Value.Year;
        int selectedmonth = this.dateTimePicker1.Value.Month;
        int selectedday = this.dateTimePicker1.Value.Day;


        int thisyear = Convert.ToInt16(DateTime.Now.Year);
        int thismonth = Convert.ToInt16(DateTime.Now.Month);
        int thisday = Convert.ToInt16(DateTime.Now.Day);


        if (cmbSchool.SelectedIndex == 0) 
        {
            MessageBox.Show("Please Select A School");
        }
        else if (cmbClassRoomName.SelectedIndex == 0)
        {
            MessageBox.Show("Please Select A Classroom");
        }
        else if (cmbClassType.SelectedIndex == 0)
        {
            MessageBox.Show("Please Select A Class Type");
        }
        else if (cmbHours.SelectedIndex == 0)
        {
            MessageBox.Show("Please Select the hour for the starting time of the class");
        }
        else if (cmbMinutes.SelectedIndex == 0)
        {
            MessageBox.Show("Please Select the minute for the starting time of the class");
        }
        else if (selectedyear < thisyear)
        {
            MessageBox.Show("Please Select a date forward from today");
        }
        else if (selectedmonth < thismonth)
        {
            MessageBox.Show("Please Select a date forward from today");
        }
        else if (selectedday < thisday)
        {
            MessageBox.Show("Please Select a date forward from today");
        }
        else if (txtLength.Text == "")
        {
            MessageBox.Show("Please enter the class length");
        }
        else
        {
            classTableAdapter.AddClass(iclassroomID, iclassTypeID, myClassDateandTime, sClassLength);
            this.Validate();
            this.classBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.geared4MathDataSet);
            MessageBox.Show("Class Added");
            this.Close();
        }
    }

    private void cmbSchool_SelectedIndexChanged(object sender, EventArgs e)
    {

        int iclassroomname = Convert.ToInt16(cmbSchool.SelectedValue);

        try
        {
            this.classRoomTableAdapter.FillBySchool(this.geared4MathDataSet.ClassRoom, iclassroomname);
            lblClassroomName.Visible = true;
            cmbClassRoomName.Visible = true;
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }

    }

http://imgur.com/FRl2Uew,0slWYAq there is my forms. http://imgur.com/FRl2Uew,0slWYAq这是我的表格。 there are 2 uploads on that link. 该链接上有2个上传。 click second page to see the second page 单击第二页查看第二页

Can't see anything obvious that'll break. 看不到任何明显的东西会破坏。
Could you provide some sample inputs? 您能否提供一些示例输入?
When you debug your code (where you get the -14,-25), are the objects the correct objects? 当您调试代码时(得到-14,-25),这些对象是正确的对象吗?

Code improvement suggestions: 代码改进建议:
Having said that, your variable names are ... lacking ... I can understand cmbClassRoomName , but all the iNames or dtNames don't need these prefixes. 话虽这么说,但您的变量名缺少...我可以理解cmbClassRoomName ,但是所有iNamesdtNames都不需要这些前缀。 Also, if you do choose to use the type and the camel case, iclassTypeID should be iClassTypeID . 另外,如果您选择使用类型和驼峰式大小写,则iclassTypeID应该是iClassTypeID Another issue is that the i prefix suggests it's an interface, not an int. 另一个问题是i前缀表明它是接口,而不是int。

About your validation if/else block ... why is it there? 关于您的验证if / else块...为什么在那里? Are you using WinForm/WPF/ASP? 您正在使用WinForm / WPF / ASP吗? Have you considered using validations? 您是否考虑过使用验证? it might be a bit funny the first time, but it's worth the time to learn in gold later on, decoupling your code so everything makes more sense. 第一次可能有点有趣,但是稍后值得花时间学习,将代码解耦,使一切变得更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM