3 Replies - 9622 Views - Last Post: 27 December 2009 - 02:35 PM Rate Topic: -----

#1 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 160
  • Joined: 17-October 09

How to go about adding all the rows from DataGridView to Listbox?

Posted 27 December 2009 - 10:38 AM

Hi there,

I am building a small application in C#. I have a database which has 5 midi files. I am using SQL CE database. I tried to create a function where you can click on a button and it will add all the files from the DataGridView to the listbox but its not working how i want it to work.

At the moment I can select an individual row and add it to listbox by using a MouseClick event on DataGridView but I would like try and do something else.

I only want the value of the first column of all the rows that exist in the grid, to be added to the listbox.

The code below is behind a button and only adds the first row but it adds the same row 5 times:

for (int Index = 0; Index < AnimalGridView1.Rows.Count; Index++)
{
		  myListBox.Items.Add(AnimalGridView1.Rows[0].ToString());
		myList.appendItem(axWindowsMediaPlayer1.newMedia(AnimalGridView1.Rows[0].Cells[0].Value.ToString()));
 }



I tried changing the Row[0]... to Rows[Index].... but it threw an exception:

Quote

Object reference not set to instance of object


I then tried changing Cells[0] to Cells[Index] as well but that threw an exception on the last line:

Quote

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


Could anyone please kindly assist me?

Thanks in advance.

This post has been edited by MarmiteX1: 27 December 2009 - 11:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to go about adding all the rows from DataGridView to Listbox?

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: How to go about adding all the rows from DataGridView to Listbox?

Posted 27 December 2009 - 12:07 PM

This should work:

private void btnFillListbox_Click(object sender, EventArgs e)
{
	foreach (DataGridViewRow row in AnimalGridView1.Rows)
	{
		if (!row.IsNewRow)
			myListBox.Items.Add(row.Cells[0].Value.ToString());
	}
}



OR this one which is exactly the same, just using for iterator instead of foreach:
for (int i = 0; i < AnimalGridView1.Rows.Count; i++)
{
	DataGridViewRow row = AnimalGridView1.Rows[i];
	if (!row.IsNewRow)
	{
		myListBox.Items.Add(row.Cells[0].Value.ToString());
	}
}




Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#3 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 160
  • Joined: 17-October 09

Re: How to go about adding all the rows from DataGridView to Listbox?

Posted 27 December 2009 - 02:34 PM

Thanks, it worked! :)
Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: How to go about adding all the rows from DataGridView to Listbox?

Posted 27 December 2009 - 02:35 PM

No problem. Anytime...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1