Link to home
Start Free TrialLog in
Avatar of linkcube1
linkcube1

asked on

Populating multiple ListViews

This is a windows form. I have a transaction form that I need a lot of help on. first off there's a textEnterISBN box that populates a listview from the text entered from the text box. I have coded this but I can't get my listview to populate from my query. I think the help is needed when the displayISBNNumber method is run. Once the first listview is populated I want another another button shown in my code as " addToReceipt" to grab the first listviews selected index and put it into the second listView which I'm not sure I have the knowledge to do. The delete button will just delete from the second listbox pretty strait forward. and the complete transaction will take the items from the second listbox and pop them into another DB and delete them from original memberBookInventoryDB. A txtTotal calculates the total from the second listbox. The query I made to retrieve the ISBN is provided along with my forms code with comments and some methods. Please any help would be great!
public partial class frmTransaction : Form
    {
        Form formParent = null;
        DateTime date = DateTime.Today;
        public frmTransaction(Form par)
        {
            formParent = par;
            InitializeComponent();
            
        }
        MemberBook mb = new MemberBook();

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
            formParent.Show();

        }

        private void frmTransaction_Load(object sender, EventArgs e)
        {
            //String.Format("[0:d]", currentDate);
            MessageBox.Show(date.ToShortDateString());

        }

        private void btnAddToReceipt_Click(object sender, EventArgs e)
        {
            //adds listview1s selected item to listview 2.
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //Deletes item from listview2 box
        }

        private void btnComplete_Click(object sender, EventArgs e)
        {
            //takes items in listView2 and inserts them in CashOut DB and deletes them from listView1s DB
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            string ISBNNumber = txtISBNNumber.Text;
            this.GetISBN(ISBNNumber);
             if (mb == null)
                {
                    MessageBox.Show("ISBNNumber not found");                   
                }
                else
                    this.DisplayISBNNumber();
            
        
            //populates listView1 with the ISBNNumber(s) returned from memberBook DB.
        }

        private void DisplayISBNNumber()
        {
            //populate listview here?
            MemberBook mb = new MemberBook();
            ListViewItem item = listView1.Items.Add(mb.ISBNNumber);
           // mb.BookID = Convert.ToInt32(item.SubItems[1].Text);
            //mb.ISBNNumber = item.SubItems[2].Text;
            //mb.Title = item.SubItems[3].Text;
            //mb.Author = item.SubItems[4].Text;
            //mb.Description = item.SubItems[5].Text;
            //mb.Condition = item.SubItems[6].Text;
            //mb.Price = Convert.ToDecimal(item.SubItems[7].Text);
        }

        private void GetISBN(string ISBNNumber)
        {
            try
            {
                mb = MemberBookInventoryDB.GetISBNNumber(ISBNNumber);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().ToString());
            }
        }
    }
}




       public static MemberBook GetISBNNumber(string ISBNNumber)
        {

              SqlConnection connection = MMABooksDB.GetConnection();
            string selectStatement
                = "SELECT BookID, ISBNNumber, Title, Author, MemberID, Price "
                + "FROM MemberBookInventory "
                + "WHERE ISBNNumber = @ISBNNumber";
            SqlCommand selectCommand =
              new SqlCommand(selectStatement, connection);
            selectCommand.Parameters.AddWithValue("@ISBNNumber", ISBNNumber);
            try
            {
                connection.Open();
                SqlDataReader reader =
                 selectCommand.ExecuteReader(CommandBehavior.Default);
                if (reader.Read())
                {
                    MemberBook mb = new MemberBook();
                    mb.BookID = (int)reader["BookID"];
                    mb.ISBNNumber = reader["ISBNNumber"].ToString();
                    mb.Title = reader["Title"].ToString();
                    mb.Author= reader["Author"].ToString();
                    mb.Member.MemberID = (int)reader["MemberID"];
                    mb.Price = (decimal)reader["Price"];
                    return mb;
                }
                else
                {
                    return null;                   
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                connection.Close();
            }
        }


    }
}

Open in new window

Avatar of muckeypuck
muckeypuck
Flag of United States of America image

First of all, have you ensured you are getting back the books from your GetISBNNumber call?

if so, the next problem i see is this:

private void DisplayISBNNumber()
        {
            //populate listview here?
            MemberBook mb = new MemberBook();
            ListViewItem item = listView1.Items.Add(mb.ISBNNumber);
           // mb.BookID = Convert.ToInt32(item.SubItems[1].Text);
            //mb.ISBNNumber = item.SubItems[2].Text;
            //mb.Title = item.SubItems[3].Text;
            //mb.Author = item.SubItems[4].Text;
            //mb.Description = item.SubItems[5].Text;
            //mb.Condition = item.SubItems[6].Text;
            //mb.Price = Convert.ToDecimal(item.SubItems[7].Text);
        }

All you are doing with this line:

ListViewItem item = listView1.Items.Add(mb.ISBNNumber);

is creating a listviewitem. it doesn't go anywhere or do anything. as a matter of fact i think i can hear it crying because it wants a listview
to call home. There's something else. A listview is highly suspect. I am not sure thats the control you want. Thats for things like pictures of folders etc.
Since you are adding an isbn number (which i am pretty sure are string values) what you want is a listBOX.

when you want to add an isbn number to the listbox (or listview if you are absolutely determined) what you want to do is

ListViewItem item = new ListViewItem(mb.ISBNNumber);
listView1.Items.Add(item);

you can also be ubercool and do it on one line like so:
listView1.Items.Add(new ListViewItem(mb.ISBNNumber));


OR for a listbox

listBox1.Items.Add(mb.ISBNNumber)

Avatar of linkcube1
linkcube1

ASKER

Thanks for the answer, I am getting the mb object but I want to populate more than one column in the first listview hence the subitems so any advice with that would be great.
Sorry, sometimes i need to pay better attention.


		private void DisplayISBNNumber()
		{
			listView1.Items.Add(new ListViewItem(new string[] {mb.ISBN, mb.Author}));

		}

Open in new window


assuming the listview.View = Details and you've added two columns

i think this is what you're saying
Yeah That what I was looking for seems to do the job. For the second list view does this work to add and populate it from a selected line from the first listview?

      private void btnAddToReceipt_Click(object sender, EventArgs e)
        {
         
                listView2.Items.Add(listView1.SelectedItems.ToString());
           
        }

the second box is receipt like doesn't need to be columned
if you need the two listbox items independent of each other:

listview2.Items.Add(listview1.Items[0].Clone();

if you want it to disappear from either listview if you delete it from 1 listview

listview2.Items.Add(listview1.Items[0]);
I'm getting this.            

System.ArgumentException was unhandled
  Message="Cannot add or insert the item '' in more than one place. You must first remove it from its current location or clone it.\r\nParameter name: item"
  Source="System.Windows.Forms"
  ParamName="item"
 
running code below

the clone code example you provided is not working for me
getting this error
Error      3      Argument '1': cannot convert from 'object' to 'System.Windows.Forms.ListViewItem'      

private void btnAddToReceipt_Click(object sender, EventArgs e)
        {
         
                //listView2.Items.Add(listView1.SelectedItems.ToString());
            //listView2.Items.Add(listView1.Items);
            listView2.Items.Add(listView1.Items[0]);
            
        }


 private void DisplayISBNNumber()
        {
            //populate listview here?
            //ListViewItem item = listView1.Items.Add(mb.ISBNNumber);
            listView1.Items.Add(new ListViewItem(new string[] { mb.ISBNNumber, mb.Author, mb.Title }));
       
        }

Open in new window

try casting it

listview2.Items.Add((ListViewItem)listview1.Items[0].Clone());
Alright that worked, Now when it clones my first listview of whatever index I select can I run a sql query and pop items in that second ListView to a new database table which will remove it from the database that populated my first listview? This is a transaction form, I want to scan an item select the item from the first list and populate it on the second one like it is now and then when a transaction is complete do something with the items in the second listview. Can I do this how this is set it?
i'm not really sure what you're asking.

You can create a trigger on the second table so that when an isbn number is inserted, delete it from the first table. That will take part of some of your requirements. You can then wait for that db call to return a flag and process when the proc returns the flag.

It depends i guess on what it is your trying to do.
My isbn is populated from my memberbook database, when an isbn is entered into listview1 it populates it. I then select one row from listview1 which now is cloned into listview2. I want put listview2's items into a separate table and then delete those items from the original database.
for each(listviewitem item in listview2
{

  process the information you need from each item to insert it into the query

}

run the query

the query i guess will look something like:

Insert into Table2
    Table2 values

Delete from table1
Where ISBN = @ISBN

or you can go the trigger route.
Ran into another problem I can only clone items[0] the first index in a column how can I do that if I select  the second or third row?
items[0] = item 1
items[1] = item 2
so on and so forth
Ok, I got a little help for another post I made. Using the code below I receive an exception just having the add method add just an item and I can't figure out how to clone the item so I have a toString for now. Is there anyway to clone this? or get around this issue?
private void btnAddToReceipt_Click(object sender, EventArgs e)
        {    
            if(listView1.SelectedItems.Count > 0)
            {  
                 foreach(ListViewItem item in listView1.SelectedItems)
                 {
                     listView2.Items.Add(item.ToString());

                 }
            }

Open in new window

private void btnAddToReceipt_Click(object sender, EventArgs e)
        {    
            if(listView1.SelectedItems.Count > 0)
            {  
                 foreach(ListViewItem item in listView1.SelectedItems)
                 {
                     listView2.Items.Add(new ListviewItem(item.ToString()));

                 }
            }
It still shows the same thing, Is there a way I can show the items without a ToString?
it shows up as

ListViewItem {"information about the item...."}
is there a way to make it just

"Information about the item"   ?
ASKER CERTIFIED SOLUTION
Avatar of muckeypuck
muckeypuck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That'll work thanks