Combobox
从SQL数据库中提取名称列,但是在选中时,我需要将dentist
列表中的其他属性填充到标记为TextBoxes
的内容中。
在If
语句中,我收到错误,突出显示的代码为dent.Name
。错误如下:
描述:不能隐式转换类型'字符串'到' int'
namespace DentistSurgery
{
public partial class Dentist_Info : Form
{
Surgery mySurgery = new Surgery();
public SqlCommand SelectCommand;
private SqlDataAdapter da;
DataRow dr;
Surgery _formsSurgery;
public Dentist_Info(Surgery SurgeryToDisplay)
{
_formsSurgery = SurgeryToDisplay;
}
public void FillCombo()
{
using (SqlConnection conn = new SqlConnection(@"Data Source = GGJG; Initial Catalog = DentistDB; Integrated Security = True"))
{
SqlCommand SelectCommand = new SqlCommand("SELECT * FROM DentistInfo", conn);
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(SelectCommand);
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
comboBox1.Items.Add(dr["DentistName"].ToString());
}
//conn.Close();
}
}
public Dentist_Info()
{
InitializeComponent();
FillCombo();
}
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
List<Dentist> dentistList = new List<Dentist>();
Dentist dent = new Dentist();
if (comboBox1.SelectedIndex = dent.Name)
{
txtDName.Text = dent.Name.ToString();
txtDSurname.Text = dent.Surname;
txtDDOB.Text = dent.DOB.ToString();
txtGender.Text = dent.Gender;
txtSalary.Text = dent.Salary.ToString();
}
}
答案 0 :(得分:2)
您没有使用相等运算符(==)。您正在使用赋值运算符(=)。因此,你得到错误的原因......
"Cannot implicitly convert type 'string' to 'int' DentistSurgery"
因为您尝试将所选索引(类型为int)分配给字符串。
这意味着您应该使用comboBox1.SelectedValue
(字符串类型)。然后你可以使用相等运算符(==)比较像这样的值......
if (comboBox1.SelectedValue == dent.Name)
{
// Do stuff here.
}
答案 1 :(得分:1)
dent
是新的空对象。 dent.Name
为空string
,您无法将string
隐式转换为integer
SelectedIndex
。
修改强>: 由于其他问题(和其他问题),这里有更多的代码。
因此,您的dentistList
为空,dentist
也为空。在这种情况下,将牙医与所选项目进行比较是没有用的。
尝试像这样重新安排代码。我还添加了一些评论:
//I'm assuming this is form
public partial class Dentist_Info : Form
{
//create list of dentists here, so that it is visible thru whole form.
private List<Dentist> dentistList = new List<Dentist>();
public Dentist_Info(Surgery SurgeryToDisplay)
{
_formsSurgery = SurgeryToDisplay;
}
public void FillComboAndListOfDentists()
{
using (SqlConnection conn = new SqlConnection(@"Data Source = GGJG; Initial Catalog = DentistDB; Integrated Security = True"))
{
SqlCommand SelectCommand = new SqlCommand("SELECT * FROM DentistInfo", conn);
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(SelectCommand);
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
comboBox1.Items.Add(dr["DentistName"].ToString());
// creating new dentist
Dentist dent = new Dentist();
dent.Name = dr["DentistName"].ToString();
dent.Surname = dr["DentistSurname"].ToString();
//etc...
//add dentist to list
dentistList.Add(dent);
}
//conn.Close();
}
}
public Dentist_Info()
{
InitializeComponent();
FillComboAndListOfDentists();
}
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//use LINQ to find specific dentist in the list (btw, you should use something unique, some kind of ID, not just Name
Dentist dent = dentistList.FirstOrDefault(d => d.Name == comboBox1.SelectedValue);
// once you have your dentist, fill all the details...
if (dent != null)
{
txtDName.Text = dent.Name.ToString();
txtDSurname.Text = dent.Surname;
txtDDOB.Text = dent.DOB.ToString();
txtGender.Text = dent.Gender;
txtSalary.Text = dent.Salary.ToString();
}
}
}
答案 2 :(得分:1)
SelectedIndex
属性的类型为int
,您希望将其与字符串值进行比较。您需要使用ToString()
方法将其转换为字符串,您还应该在==
语句中使用等于运算符=
而不是赋值运算符if
,如下所示:
if (comboBox1.SelectedIndex.ToString() == dent.Name)
{
....
但基于dent.Name
,我认为您应该在此使用SelectedItem
属性,但因为SelectedItem
类型为object
,您应该使用ToString()
:< / p>
if (comboBox1.SelectedItem.ToString() == dent.Name)
{
...
答案 3 :(得分:1)
更好的方法是使用selected Value
从comboBox
获取comboBox1.SelectedValue
,然后将其与dentName进行比较。
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
List<Dentist> dentistList = new List<Dentist>();
Dentist dent = new Dentist();
if (comboBox1.SelectedValue == dent.Name)
{
txtDName.Text = dent.Name.ToString();
txtDSurname.Text = dent.Surname;
txtDDOB.Text = dent.DOB.ToString();
txtGender.Text = dent.Gender;
txtSalary.Text = dent.Salary.ToString();
}
}