1

I would like to display the record from database to c# in listview. When I display the record into 2 different listview, the record in the second listview does not appear.

In listview1, i would like to display the record where package = textbox4.text, then in listview2, i would like to display the record where package = textbox5.text.

Here is my line of code, only the records in first listview display.

Please help me. Thank you

//code for first listview
MySqlConnection.Open();
SqlCommand command1 = new SqlCommand("Select * from reservation_inventory where package='"+textBox4.Text+"'", MySqlConnection);

p_table.Clear();
SqlDataAdapter m_da = new SqlDataAdapter("Select * from reservation_inventory where package='" + textBox4.Text + "'", MySqlConnection);

m_da.Fill(p_table);
listView1.Items.Clear();

for (int i = 0; i < p_table.Rows.Count; i++)
{
    DataRow drow = p_table.Rows[i];

    if (drow.RowState != DataRowState.Deleted)
    {
        ListViewItem lvi = new ListViewItem(drow["id"].ToString());
        lvi.SubItems.Add(drow["equipment"].ToString());
        lvi.SubItems.Add(drow["status"].ToString());
        listView1.Items.Add(lvi);
    }
}

// code for second listview
SqlCommand command2 = new SqlCommand("Select * from reservation_inventory where package='" + textBox5.Text + "'", MySqlConnection);

p_table.Clear();
SqlDataAdapter m_da2 = new SqlDataAdapter("Select * from reservation_inventory where package='" + textBox5.Text + "'", MySqlConnection);

m_da.Fill(p_table);
listView2.Items.Clear();

for (int i = 0; i < p_table.Rows.Count; i++)
{
    DataRow drow = p_table.Rows[i];

    if (drow.RowState != DataRowState.Deleted)
    {
        ListViewItem lvi = new ListViewItem(drow["id"].ToString());
        lvi.SubItems.Add(drow["equipment"].ToString());
        lvi.SubItems.Add(drow["status"].ToString());
        listView1.Items.Add(lvi);
    }
}
1
  • why loop the objects making list view items... just go listView2.DataSource = p_table; listView2.DataBind(); Commented Mar 10, 2014 at 13:13

1 Answer 1

2

In your second loop change

listView1.Items.Add(lvi);

to

listView2.Items.Add(lvi);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.