我是新来的,我有点迷路。 试图通过在组合框中选择来显示我的数据库在文本框中的值。但我不能。 请帮帮我。这是我的代码:如何显示文本框C#中的组合框选定值?
private void CargarDatos()
{
string consulta = "SELECT * FROM [dbo].[alumno]";
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(Properties.Settings.Default.conexion);
SqlCommand cmd = new SqlCommand(consulta, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
con.Open();
da.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
con.Close();
this.dataGridView1.DataSource = dt;
cbalumno.DataSource = dt;
cbalumno.DisplayMember="Nombre";
cbalumno.ValueMember="Id";
}
private void Form1_Load(object sender, EventArgs e)
{
CargarDatos();
}
private void cbalumno_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
我想要显示的参数是表名的“姓名”“姓”和“DNI”。
任何想法,我该怎么做?
如果发生错误,请先将'con.Close();'移至'finally'块以关闭连接。现在实际上你需要多值组合框,根据'DataTable'显示多个列,你必须为你设计一个,或者你可以使用DevExpress ['LookUpEdit'](https://documentation.devexpress.com/#windowsforms/clsDevExpressXtraEditorsLookUpEdittopic) –