3
Answers

Geting error: index was outside the bounds of the array

Photo of Anish Khan

Anish Khan

15y
5.8k
1

Hi All,
this is my code to store multiple selected item from a list box in windows application and I am geting error.
Already i have declared array size.
string[] ItemArray = new string[20];
for (int i = 0; i <= listBox1.SelectedItems.Count; i++)
{
txtmessage.Text = ((DataRowView)listBox1.SelectedItem).Row.ItemArray[i].ToString();
}
Please help me
Thanks

Answers (3)

1
Photo of Bryian Tan
NA 9.4k 887.4k 15y
Hello,

You have to subtract one from the count, because 0 to 19 = 20 counts

string[] ItemArray = new string[20];
for (int i = 0; i <= listBox1.SelectedItems.Count - 1; i++)
{
txtmessage.Text = ((DataRowView)listBox1.SelectedItem).Row.ItemArray[i].ToString();
}


Thanks,
Bryian Tan
Accepted
1
Photo of Anish Khan
NA 25 0 15y

Hi Bryian,
Your answer is good...
But  listBox1.SelectedItems.Count is < 5
and also I am geting error..

Thanks

Anish

0
Photo of Bryian Tan
NA 9.4k 887.4k 15y
Anish,

What is the relationship between the listbox1, ItemArray and the DataView?