Click here to Skip to main content
15,904,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one checkedlistbox and datagridview, i want to select multiple items from checkedlistbox and the corresponding data need to fetch from database and display in datagridview.

It is working absolutely correct when i select one of the item from checkedlistbox, when i select two item then it shows only latest item selected values.

What I have tried:

C#
for (int j = 0; j <= i - 1; j++)
{
    string s = "select * from Final where Assigrp ='" + checkedListBox2.CheckedItems[j].ToString() + "'";
    sda = new SqlDataAdapter(s, con);
    ds = new DataSet();
    SqlCommandBuilder scb = new SqlCommandBuilder(sda);
    sda.Fill(ds, "Final");
    bsource.DataSource = ds.Tables["Final"];
    dataGridView1.DataSource = bsource;

}
Posted
Updated 21-Oct-16 5:59am
Comments
[no name] 21-Oct-16 11:50am    
Yes... you need to add all of the selected items to your query if that is what you want to do.
Member 12673779 21-Oct-16 11:58am    
I need to design like
example:
Suppose if i selected "ABC" from checkedlistbox corresponding data fetch from database and display in datagridview, if i select "ABC" and "XYZ" it should fetch ABC and XYZ data from database and display in datagridview..

And if i unchecked "ABC", ABC data should remove from datagridview .

Hope my question is clear...
Karthik_Mahalingam 27-Oct-16 1:48am    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

try this

C#
string where = "";
           foreach (object item in checkedListBox1.CheckedItems)
               where += string.Format("'{0}',", item);
           where = where.TrimEnd(',');
           string query = "select * from Final where Assigrp in ({0})";
           string s = string.Format(query, where);
           sda = new SqlDataAdapter(s, con);
 
Share this answer
 
Comments
Member 12673779 7-Nov-16 18:06pm    
Thank you thank you thank you sooo much
it worked for me..
Karthik_Mahalingam 8-Nov-16 2:07am    
welcome :)
Your sql WHERE is getting a single item. Use WHERE Assigrp IN () and build your list of ids that way.
 
Share this answer
 
Comments
Member 12673779 21-Oct-16 12:06pm    
But for loop is taking only one value and display that in datagridview..
If i use Where Assigrp IN () it is showing same result..
And if i remove for loop and use Where Assigrp IN () it is not showing data in datagridview.
ZurdoDev 21-Oct-16 12:07pm    
Use your loop to build a list of IDs and then call your sql one time using the WHERE IN () statement.
Member 12673779 21-Oct-16 12:17pm    
can you please share example..
Member 12673779 24-Oct-16 8:40am    
I tried below code to store checkeditems, the below code is storing checked items from checkedlistbox but my items are stored in different rows not in single row in table (database).

can you please help me to search items row by row..

string str = "";
if ( checkedListBox1.CheckedItems.Count > 0)
{
for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
str += checkedListBox2.CheckedItems[i].ToString();
}
}

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