Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can retrieve the data I want in LINQPad.

Trying to get it programmatically *and* assign it to a CheckedListBox in C#, though, has not yet worked. My code is:

C#
var sqlDal = new SQLServer(Properties.Settings.Default.PltypusDataConnection) { CommandTimeout = 120 };
DataSet dsMembers = sqlDal.runSQLDataSet("SELECT PoisonToeLength FROM Platypi ORDER BY PoistonToeLength", "PoisonToes");
var dtMembers = dsMembers.Tables[0];
MemberListBox.DataSource = dtMembers;


All this does is fill the CheckedListBox (MemberListBox) with a slew of items displaying "System.Data.DataRowView"

How can I display the values from the query in the CheckedListBox?
Posted
Comments
Krunal Rohit 4-Nov-15 12:08pm    
Umm, where is DisplayMember and ValueMember (if I remember it correctly) ?

-KR
Richard Deeming 4-Nov-15 12:09pm    
Which CheckedListBox are you using? WPF? Windows Forms? ASP.NET Web Forms? ASP.NET MVC? Something else?
B. Clay Shannon 4-Nov-15 12:10pm    
Windows Forms; I found a solution, thanks (below).

1 solution

I found out how to do it here ; so this works:

C#
string constring = Properties.Settings.Default.CPSDataConnection;
using (SqlConnection con = new SqlConnection(constring))
{
    using (SqlCommand cmd = new SqlCommand("SELECT Unit FROM MasterUnits ORDER BY Unit", con))
    {
        cmd.CommandType = CommandType.Text;
        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
        {
            DataTable dt = new DataTable();
            sda.Fill(dt);
            ((ListBox)MemberListBox).DataSource = dt;
            ((ListBox)MemberListBox).DisplayMember = "Unit";
            ((ListBox)MemberListBox).ValueMember = "Unit";
        }
    }
}
 
Share this answer
 
Comments
Krunal Rohit 4-Nov-15 12:12pm    
I was close to the solution. :doh:

-KR

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