这是我的代码:
sql = "SELECT * FROM stock";
cmd = new SQLiteCommand(sql, mdb);
SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
var_depot = reader["depot"].ToString();
ajouterlistview1 = new ListViewItem(reader["id"].ToString());
ajouterlistview1.SubItems.Add(reader["article"].ToString());
listView1.Items.Add(ajouterlistview1);
listView1.Refresh();
for (int i = 0; i < listView1.Columns.Count; i++)
{
if (var_depot == listView1.Columns[i].Text)
{
for (int j = 0; j < listView1.Items.Count; j++)
{
/*if (listView1.Columns[i].Text == var_depot )
{*/
listView1.Items[j].SubItems[1].Text = reader["quantite"].ToString();
//}
}
}
}
}
此代码工作正常,但是当我更改此行时:
listView1.Items[j].SubItems[1].Text = reader["quantite"].ToString();
到这一行:
listView1.Items[j].SubItems[i].Text = reader["quantite"].ToString();
我收到错误,因为我的行X未定义:/
我该怎么办?
答案 0 :(得分:0)
看起来您有listView1.Columns
而不是listView1.Items[...].SubItems
。添加一些空的子项(ajouterlistview1.SubItems.Add("")
)以确保您可以访问listView1.Items [...] .SubItems [i] .Text。
尝试这种方式:
for (int i = 0; i < listView1.Columns.Count; i++)
{
if (var_depot == listView1.Columns[i].Text)
{
ajouterlistview1.SubItems.Add(reader["quantite"].ToString());
break;
}
else
ajouterlistview1.SubItems.Add("");
}