C# 如何在c中显示名称并从组合框中插入值(名称的ID)
提前谢谢你们。我是c Windows窗体的新用户 我有一张有身份证和名字的桌子 ID | Name --------------- 1 | Lion 2 | Tiger 3 | Crocodile 它尝试插入组合框中显示的内容,它是名称,而不是id 在PHP中,如下所示: 这将插入id并显示名称。但是我该怎么用c语言呢?C# 如何在c中显示名称并从组合框中插入值(名称的ID),c#,winforms,combobox,C#,Winforms,Combobox,提前谢谢你们。我是c Windows窗体的新用户 我有一张有身份证和名字的桌子 ID | Name --------------- 1 | Lion 2 | Tiger 3 | Crocodile 它尝试插入组合框中显示的内容,它是名称,而不是id 在PHP中,如下所示: 这将插入id并显示名称。但是我该怎么用c语言呢? 再次感谢您抽出时间 您可以添加自定义类型,而不是将名称字符串添加到组合框中: class Animal { public int ID { get; s
再次感谢您抽出时间 您可以添加自定义类型,而不是将名称字符串添加到组合框中:
class Animal
{
public int ID { get; set; }
public string Name { get; set; }
public override string ToString()
{
return Name;
}
}
为每个条目var animal=new animal{ID=intdr[0],Name=stringdr[1]}创建该类型的对象;,将对象添加到组合框中。然后,当您要检索它时,只需将该项目转换为动物类型并获取ID
var animal = (Animal)comboBox1.SelectedItem;
如果我正确理解了你的问题,这应该符合你的要求: 用这样的东西加载组合框我现在无法测试,所以我可能犯了一些拼写错误或小语法错误: **更新:好的。这是我最后一次在匆忙中回答问题。我的原始代码充满了问题和愚蠢的打字错误。我真诚的道歉!下面的代码包含您尝试执行的所有操作的基本版本。您可能需要调整它以适应您的需要 一些建议: A.如图所示,将连接和命令放在使用块中 B.不要在代码中硬编码连接字符串,而是使用解决方案资源管理器左侧的Properties.Settings designer,并为连接字符串创建一个中心引用。然后在代码中引用它,如图所示 以下内容执行您试图实现的基本功能,并在我的机器上运行:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += new EventHandler(button1_Click);
this.FillDropDownList();
}
void button1_Click(object sender, EventArgs e)
{
this.SaveComboBoxContent();
}
public void FillDropDownList()
{
string SQL = "SELECT id, name FROM info ORDER BY name";
DataTable dt = new DataTable();
// Set the connection string in the Solutions Explorer/Properties/Settings object (double-click)
using (var cn = new SqlConnection(Properties.Settings.Default.MyConnectionString))
{
using(var cmd = new SqlCommand(SQL, cn))
{
cn.Open();
try
{
dt.Load(cmd.ExecuteReader());
}
catch (SqlException e)
{
// Do some logging or something.
MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
}
}
}
// UPDATED - The .ValueMember and .DisplayMember properties
// refer to the string name of the field (oops!):
comboBox1.DataSource = dt;
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
}
public void SaveComboBoxContent()
{
string SQL = "INSERT INTO info2 (name_id) VALUES (@name_id)";
using (var cn = new SqlConnection(Properties.Settings.Default.MyConnectionString))
{
using(var cmd = new SqlCommand(SQL, cn))
{
cmd.Parameters.AddWithValue("@name_id", comboBox1.SelectedValue);
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
// Do some logging or something.
MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
}
}
}
}
}
希望有帮助。您有SQL注入漏洞。Metro?WinForms?WPF?银灯?Windows Phone?ASP.Net?MonoTouch?SLaks:对不起,这是Windows窗体谢谢你,我和其他人。今天我病得很重,明天我会试试。换句话说,你问了四个问题,但只接受了其中一个的答案。这告诉人们,当他们花时间回答你的问题时,你不太可能承认他们的努力。当然,你只能接受一个答案。但游戏化是乐趣的一部分,也是动力的一部分。我明白了……谢谢你。我会的。这给了我一些错误的答案。在公共void FillDropDownListstring查询中,我看到了组合框dropdownname…谢谢。我会的。这给了我一些错误的答案。在FillDropDownListstring查询中,ComboBox DropDownName在DropDownName.ValueMember=dt[id]处给了我一个错误;DropDownName.DisplayMember=dt[name]。它说不能对类型为“system.Data.DataTable”的表达式应用带[]的索引,然后在公共空间中使用SaveComboBoxContent进行重试{cmd.ExecuteNonQuery;//这里的givs-me错误类似于“只能使用赋值、调用、增量、减量和新对象表达式作为声明”您可以编辑您的帖子并向我显示您当前的代码吗?哦,顺便说一句,我搞砸了。在我发布的代码中,cmd.ExecuteNonQuery;应该是cmd.ExecuteNonQuery请记住我关于不被删除的警告能够在我发布代码时测试代码吗?-请参阅已编辑的代码。
var animal = (Animal)comboBox1.SelectedItem;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += new EventHandler(button1_Click);
this.FillDropDownList();
}
void button1_Click(object sender, EventArgs e)
{
this.SaveComboBoxContent();
}
public void FillDropDownList()
{
string SQL = "SELECT id, name FROM info ORDER BY name";
DataTable dt = new DataTable();
// Set the connection string in the Solutions Explorer/Properties/Settings object (double-click)
using (var cn = new SqlConnection(Properties.Settings.Default.MyConnectionString))
{
using(var cmd = new SqlCommand(SQL, cn))
{
cn.Open();
try
{
dt.Load(cmd.ExecuteReader());
}
catch (SqlException e)
{
// Do some logging or something.
MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
}
}
}
// UPDATED - The .ValueMember and .DisplayMember properties
// refer to the string name of the field (oops!):
comboBox1.DataSource = dt;
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
}
public void SaveComboBoxContent()
{
string SQL = "INSERT INTO info2 (name_id) VALUES (@name_id)";
using (var cn = new SqlConnection(Properties.Settings.Default.MyConnectionString))
{
using(var cmd = new SqlCommand(SQL, cn))
{
cmd.Parameters.AddWithValue("@name_id", comboBox1.SelectedValue);
cn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException e)
{
// Do some logging or something.
MessageBox.Show("There was an error accessing your data. DETAIL: " + e.ToString());
}
}
}
}
}