我正在寫一個程序,我添加一個數學類到sql,但使用組合框的一些外鍵,以便他們只能從某些參考表中選擇項目(例如,類名來自不同的表,所有的類名都在其中。但是由於某種原因,當我使用combobox.selectedvalue函數時,classnameid沒有給出正確的id,它繼續給出奇怪的-14和-25作爲id這是錯誤的。請幫助。這裏是我的代碼c#代碼過濾一個組合框與一個paramatized組合框
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 有我的方式。有2周的上傳鏈接。點擊第二頁看到第二頁
它的btnaddClass按鈕有問題 –