更新不同形式的listview c#

时间:2016-09-04 05:37:14

标签: c# listview

我想更新listview上的记录,但它没有更新。

listview位于form1,文本框位于form2。错误是

InvalidArgument=Value of '0' is not valid for 'index'.

请帮助。

Form1中:

private void btnupdate_Click(object sender, EventArgs e)
{
    UpdateBook ub = new UpdateBook();

    ub.txtbook.Text = lvbooks.SelectedItems[0].SubItems[0].Text;
    ub.txtauthor.Text = lvbooks.SelectedItems[0].SubItems[1].Text;
    ub.txtpublisher.Text = lvbooks.SelectedItems[0].SubItems[2].Text;
    ub.txtyear.Text = lvbooks.SelectedItems[0].SubItems[3].Text;
    ub.txtcategory.Text = lvbooks.SelectedItems[0].SubItems[4].Text;
    ub.txtisbn.Text = lvbooks.SelectedItems[0].SubItems[5].Text;
    ub.txtquantity.Text = lvbooks.SelectedItems[0].SubItems[6].Text;

    ub.ShowDialog();
}

窗体2:

private void btnupdate_Click(object sender, EventArgs e)
{
    books b = new books();

    exequery("Update tbl_books set NameOfBook = '" + txtbook.Text + "',Author = '" + txtauthor.Text + "',Publisher='" + txtpublisher.Text + "',YearPublished = '" + txtyear.Text + "',Category='" + txtcategory.Text + "',ISBN = '" + txtisbn.Text + "',TotalNumberOfBooks = '" + txtquantity.Text + "' where NameOfBook = '"+ b.lvbooks.SelectedItems[0].Text +"'");

    MessageBox.Show("Item has been Updated!");
    showlv("Select * from tbl_books", b.lvbooks);
    this.Hide();
    b.ShowDialog();
}

1 个答案:

答案 0 :(得分:0)

确保在更新前选择了一些内容。所以首先检查是否有任何选择。

     If (ListView1.SelectedItems.Count.    > 0)
 {
 Update ();
 }
 else {
 MessageBox.Show ("Nothing  was.     selected ");
 }