Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i'm trying here to filter dataGridView with text checked from checkedlistBox wich is linked with column table from database !

an error "Index was outside the bounds of the array" shows when i click on the button at the line :


C#
string name = ((DataRowView)checkedListBox1.CheckedItems[j])["Name"].ToString();


i tried to test my code with :

MessageShow (name); to get know where THE PROBLEM START EXACTLY , but it shows me correctly the text that i have checked from the list in the MessageShow and than the Mesage Error return to show at this line code :


C#
string name = ((DataRowView)checkedListBox1.CheckedItems[j])["Name"].ToString();



Thank you for help !
SORRY FOR MY BAD ENGLISH I WISH THAT I EXPLAINED THE PROBLEM CLEARELY !

What I have tried:

private void button1_Click(object sender, EventArgs e)
        {
            

             for (int j = 0; j < checkedListBox1.Items.Count; ++j)
                

             {

                    string name = ((DataRowView)checkedListBox1.CheckedItems[j])["Name"].ToString();
                    MessageBox.Show(name);

                    BindingSource bs = new BindingSource();
             
                    bs.DataSource = dataGridView1.DataSource;

                   
                    bs.Filter = "Convert(nomFormationTC ,'System.String') like '%" + t + "%'";
                   
                    dataGridView1.DataSource = bs;

               }
 
        }
Posted
Updated 1-Feb-17 22:54pm
v2

You are accessing checkedListBox1.CheckedItems with index [j] from enumeration of checkedListBox1.Items. You probably want to iterate checkedListBox1.CheckedItems instead.
 
Share this answer
 
Comments
EM_Y 2-Feb-17 4:54am    
It solved my problem
Thank you ^^
jimmson 2-Feb-17 4:58am    
You are welcome. Happy coding!
One thing I think causes the problem is that you are checking against the size of checkedListBox1.Items and you are trying to get the Name attribute of checkedListBox1.CheckedItems, which are not guaranteed to be of the same size and thus it terminates with this error.

Index out of bounds, exception means that the index you passed to fetch the value was not in the pool of index, which is starting from 0 and goes to 1 less than the number of elements; in list of 10 elements, valid index numbers are 0-9. You need to change it to something like this,
C#
for (int j = 0; j < checkedListBox1.CheckedItems.Count; ++j)
This would work, because the iteration will be done based on only the elements that are in CheckedItems list. You might also want to consider using, foreach structure.
C#
// Get the items in the checked items list
foreach (var checkedItem in checkedListBox1.CheckedItems)
{
    // please check the casting, as I did not consider the casting here. 
    string name = checkedItem["Name"].ToString();
    MessageBox.Show(name);

    .. 

This will also help in many cases, to overcome the exception.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900