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
Geting error: index was outside the bounds of the array
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Bryian TanRank #396 · 6.3K reputationPosted Feb 9, 2010, 9:25 PM EST
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
Anish Khan25 reputationPosted Feb 9, 2010, 11:18 PM EST
Your answer is good...
But listBox1.SelectedItems.Count is < 5
and also I am geting error..
Thanks
Anish
Bryian TanRank #396 · 6.3K reputationPosted Feb 10, 2010, 4:26 AM EST
What is the relationship between the listbox1, ItemArray and the DataView?