我有一个补货标签,在该标签内我有一个listview
,它会显示数量低于5的产品,所以点击一行后,编辑并点击更新后,它不会被移除listview
。即使我已经编辑了数量。
以下是我的列表视图中的代码:
SqlConnection MySqlConnection;
DataTable p_table = new DataTable();
MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
MySqlConnection.Open();
SqlCommand command1 = new SqlCommand("Select * from inventory where qt <= 5", MySqlConnection);
//Clear the datatable to prevent duplicate generation of data in gridview.
p_table.Clear();
SqlDataAdapter m_da = new SqlDataAdapter("Select * from inventory where qt <= 5", MySqlConnection);
m_da.Fill(p_table);
// Display items in the ListView control
for (int i = 0; i < p_table.Rows.Count; i++)
{
DataRow drow = p_table.Rows[i];
// Only row that have not been deleted
if (drow.RowState != DataRowState.Deleted)
{
// Define the list items
//(drow["bnum"].ToString());
ListViewItem lvi = new ListViewItem(drow["pnum"].ToString());
lvi.SubItems.Add(drow["pname"].ToString());
lvi.SubItems.Add(drow["descr"].ToString());
lvi.SubItems.Add(((DateTime)drow["dater"]).ToShortDateString());
//lvi.SubItems.Add(drow["exp"].ToString());
lvi.SubItems.Add(((DateTime)drow["exp"]).ToShortDateString());
lvi.SubItems.Add(drow["qt"].ToString());
// Add the list items to the ListView
listView4.Items.Add(lvi);
}
}
}
以下是我的更新按钮上的代码:
SqlConnection MySqlConnection;
DataTable p_table = new DataTable();
MySqlConnection = new SqlConnection("Data Source=christina\\sqlexpress;Initial Catalog=cafe_inventory;User ID=sa;Password=tina;");
MySqlConnection.Open();
string pnum, pname, descr, qt, interval;
//DateTime dater;
pnum = pntext.Text;
pname = nametext.Text;
descr = desctext.Text;
//dater = drtext.Value;
//exp = detext.Text;
qt = quantitytext.Text;
interval = diff.Text;
DateTime dater = drtext.Value.Date;
DateTime exp = detext.Value.Date;
SqlDataReader m_dr;
SqlCommand command = new SqlCommand("Update [inventory] set pname = '" + pname + "',descr = '" + descr + "',dater = '" + dater + "',exp = '" + exp + "',qt = '" + qt + "',interval = '" + interval + "' where pnum = '" + pnum + "'", MySqlConnection);
m_dr = command.ExecuteReader();
m_dr.Close();
listView4.SelectedItems.Clear();
listView3.View = View.Details;
command = new SqlCommand("Select * from inventory", MySqlConnection);
m_dr = command.ExecuteReader();
while (m_dr.Read())
{
//ListViewItem lv = new ListViewItem(m_dr["bnum"].ToString());
ListViewItem lv = new ListViewItem(m_dr["pnum"].ToString());
lv.SubItems.Add(m_dr["pname"].ToString());
lv.SubItems.Add(m_dr["descr"].ToString());
//lv.SubItems.Add(m_dr["dater"].ToString());
lv.SubItems.Add(((DateTime)m_dr["dater"]).ToShortDateString());
//lv.SubItems.Add(m_dr["exp"].ToString());
lv.SubItems.Add(((DateTime)m_dr["exp"]).ToShortDateString());
lv.SubItems.Add(m_dr["qt"].ToString());
lv.SubItems.Add(m_dr["interval"].ToString());
listView3.Items.Add(lv);
}
MessageBox.Show("Record Updated!");
}
谢谢。