如何从table1中的2comboboxes中选择2列,并将这些2列中的数据放入listview11中的listview11中
我尝试了这个但是没有工作没有添加到listview1
private void button1_Click(object sender, EventArgs e)
{
if (cn.State == ConnectionState.Closed) cn.Open();
cm.Connection = cn;
if (comboBox3.Enabled == true)
{
string searchFor2 = comboBox1.Text;
string searchFor3 = comboBox2.Text;
string selectSql = "SELECT " + searchFor2 + ", " + searchFor3 + " FROM itmsparts";
SqlCommand com = new SqlCommand(selectSql, cn);
try
{
using (SqlDataReader read1 = com.ExecuteReader())
{
using (SqlDataReader read1 = com.ExecuteReader())
{
while (read1.Read())
{
ListViewItem parent = listView1.Items.Add(read1[0].ToString());
parent.SubItems.Add(read1[1].ToString());
}
}
}
}
finally
{
}
}
}
答案 0 :(得分:0)
您无法使用read1["searchFor2"]
获取字段名称
尝试:
foreach (ListViewItem item in listView1.Items)
{
item.Text = (read1[0].ToString());
item.SubItems[1].Text = (read1[1].ToString());
}
答案 1 :(得分:0)
当你说
时READ1 [" searchFor2"]
这意味着它会尝试搜索名称为' searchFor2'的列。你需要删除双引号。
item.Text = (read1[searchFor2].ToString());
item.SubItems[1].Text = (read1[searchFor3].ToString());
答案 2 :(得分:0)
using (SqlDataReader read1 = com.ExecuteReader())
{
while (read1.Read())
{
ListViewItem parent = listView1.Items.Add(read1[0].ToString());
parent.SubItems.Add(read1[1].ToString());
}
}
必须改变那部分并且它正在工作:)