Shiloh
Shiloh

Reputation: 35

How can I list table columns (returned through an SQL query) as items in a CheckedListBox?

If I have the following table:

 canAssign 
------------
     1       

Is there a way to add the column header text (e.g., canAssign, etc.) to the CheckedListBox as the labels that a user can check?

All answers I've found list the value as the labels, like this:

☐ 1

Instead of this:

☐ canAssign

For Example Only, If I'm using the following to list whatever value is in the canAssign column, how could I change this to list the 'canAssign' column header text?

string myString = "SELECT canAssign FROM Permissions";
using (SqlConnection myConn = new SqlConnection(globalConnectionString)) 
{
    try {
        myConn.Open();
        using (SqlCommand myComm = new SqlCommand(myString, myConn)) 
        {
            SqlDataReader myReader = myComm.ExecuteReader();
            while (myReader.Read()) {
                checkedListBox1.Items.Add(myReader["canAssign"]);
            }
        }
    } catch (Exception ex) {
        MessageBox.Show(ex.Message);
    }
}

Upvotes: 3

Views: 840

Answers (2)

user10216583
user10216583

Reputation:

Assuming the SQL query in your code snippet is to get the permissions of a specific user and display them in a CheckedListBox using the same fields names from the database.

If that sounds right, read the entry, loop to get the fields names and values through the SqlDataReader.GetName and SqlDataReader.GetBoolean methods respectively.

//For example...
var myString = "SELECT * FROM Permissions WHERE UserId = ....";

try
{
    using (SqlConnection myConn = new SqlConnection(globalConnectionString))
    using (SqlCommand myComm = new SqlCommand(myString, myConn))
    {
        myConn.Open();

        using (var myReader = myComm.ExecuteReader())
            if (myReader.Read())
                for (var i = 0; i < myReader.FieldCount; i++)
                    checkedListBox1.Items.Add(myReader.GetName(i), myReader.GetBoolean(i));
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Upvotes: 1

J&#243;zef Podlecki
J&#243;zef Podlecki

Reputation: 11283

In this case I think you'd like to have custom objects in CheckedListBox.Items collection and use CheckedListBox.DisplayMember property with CheckedListBox.ValueMember

DisplayMember

Gets or sets a string that specifies a property of the objects contained in the list box whose contents you want to display.

ValueMember

Gets or sets a string that specifies the property of the data source from which to draw the value.

Example

public class ListBoxItem
{
    public string Text { get; set; }
    public string Value { get; set; }
}

checkedListBox.DisplayMember = "Text";
checkedListBox.ValueMember = "Value";

...create connection and create command logic...
command.CommandText = "SELECT * FROM Permissions";
var reader = command.ExecuteReader();

while (reader.Read()) {

  // of course it would be better to cache that and go straight by indexes
  for(int i = 0; i < reader.FieldCount; i++) {
     var columnName = reader.GetName(i);
     // some logic to humanize values like 'canDownload' to 'Can Download'
     var label = getLabelFor(columnName);
     var value = reader.GetBoolean(i);
     checkedListBox.Items.Insert(0, new ListBoxItem(label , value));
  }
}

Upvotes: 1

Related Questions