我有2个特定列的列表视图,但我不能用它做减法。在列表视图1中,我有7列,列表视图2中的5列。我想在listview 2中使用“banyak”在listview 1中减去列“banyak”。当我输入此代码时,数据不会出现在listview中。但我可以编译该程序。请帮我!这是我的代码:
在listview 1中
private void button3_Click(object sender, EventArgs e)
{
try
{
con.Open();
cmd.Connection = con;
cmd.CommandText = "select nama_peminjam,nobp_nip,nohp,nama_komponen,jenis_komponen,peminjaman_alat.banyak,tanggal from db_coba.peminjaman_alat,db_coba.list_komponen where db_coba.peminjaman_alat.komponen_id=db_coba.list_komponen.komponen_id";
cmd.ExecuteNonQuery();
baca = cmd.ExecuteReader();
peminjaman_alat.Items.Clear();
while (baca.Read())
{
ListViewItem item = new ListViewItem(baca["nama_peminjam"].ToString());
item.SubItems.Add(baca["nobp_nip"].ToString());
item.SubItems.Add(baca["nohp"].ToString());
item.SubItems.Add(baca["nama_komponen"].ToString());
item.SubItems.Add(baca["jenis_komponen"].ToString());
item.SubItems.Add(baca["banyak"].ToString());
item.SubItems.Add(baca["tanggal"].ToString());
peminjaman_alat.Items.Add(item);
}
con.Close();
}
catch (Exception ex)
{
con.Close();
}
}
列表视图2中的
private void button8_Click(object sender, EventArgs e)
{
try
{
con.Open();
cmd.Connection = con;
cmd.CommandText = "select nama_komponen,list_komponen.status,jenis_komponen,nfc_tag,list_komponen.banyak,peminjaman_alat.banyak from db_coba.list_komponen,db_coba.peminjaman_alat where db_coba.list_komponen.komponen_id=db_coba.peminjaman_alat.komponen_id";
cmd.ExecuteNonQuery();
baca = cmd.ExecuteReader();
listkomponen.Items.Clear();
while (baca.Read())
{
ListViewItem item = new ListViewItem(baca["nama_komponen"].ToString());
item.SubItems.Add(baca["jenis_komponen"].ToString());
item.SubItems.Add(baca["nfc_tag"].ToString());
item.SubItems.Add(baca["banyak"].ToString());
item.SubItems.Add(baca["status"].ToString());
if (peminjaman_alat.SelectedIndices.Count == 0) return;
string banyak_peminjaman = peminjaman_alat.SelectedItems[0].SubItems[3].Text;
item.SubItems[3].Text = (Int32.Parse(item.SubItems[3].Text) - Int32.Parse(banyak_peminjaman)).ToString();
listkomponen.Items.Add(item);
}
con.Close();
}
catch (Exception ex)
{
con.Close();
}
}
我希望你能理解我的意思。
答案 0 :(得分:0)
只需检查此功能。参考链接MSDN 示例图像参考
public void AddItemsOne()
{
// Set the view to show details.
listView1.View = View.Details;
ListViewItem item1 = new ListViewItem("item1",0);
// Place a check mark next to the item.
//item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
item1.SubItems.Add("4");
// Create columns for the items and subitems.
// Width of -2 indicates auto-size.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Item Sub Column One", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Item Sub Column Two", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Item Sub Column Three", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Item Sub Column Four", -2, HorizontalAlignment.Left);
//Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[] { item1 });
// Add the ListView to the control collection.
this.Controls.Add(listView1);
}