joao costa joao costa - 1 year ago 50
SQL Question

showing rows in a combo box

I need to show some rows ofa database that I have and show them inside a combo box.

I've this piece of code that is working but instead of showing the correct rows it just shows

System.Data.DataRow
.

Here is my code:

public Form1()
{
InitializeComponent();
string setting = ConfigurationManager.AppSettings["setting1"];
string conn = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
using (SqlConnection sqlConn = new SqlConnection(conn))
{
string sqlQuery = @"SELECT CDU_ESTADOS from testetiposdestados";
MessageBox.Show(sqlQuery);
SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
da.Fill(table);
comboBox1.DataSource = new BindingSource(table, null);

}

}


Here is a screenshot of what the combo box shows:

enter image description here

And here what the combo box should show:

enter image description here

If you have any question about the code or something else feel free to ask

Answer Source

You need to set the combobox DisplayMember property. Otherwise it just displays the value returned from the ToString() of the data items (in this case, System.Data.DataRow):

combobox1.DisplayMember = "CDU_ESTADOS";

To put in context of your code:

public Form1()
{
    InitializeComponent();
    string setting = ConfigurationManager.AppSettings["setting1"];
    string conn = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
    using (SqlConnection sqlConn = new SqlConnection(conn))
    {
        string sqlQuery = @"SELECT CDU_ESTADOS  from  testetiposdestados";
        MessageBox.Show(sqlQuery);
        SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable table = new DataTable();
        da.Fill(table);
        combobox1.DisplayMember = "CDU_ESTADOS";
        comboBox1.DataSource = new BindingSource(table, null);
    }
}

btw, SqlCommand, SqlDataAdapter, and also DataTable all implements the IDisposable interface - so you should use them with a using statement. Also, I would suggest start using the var keyword for local parameters, it will make your code shorter and more readable:

public Form1()
{
    InitializeComponent();
    var setting = ConfigurationManager.AppSettings["setting1"];
    var conn = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
    using (var sqlConn = new SqlConnection(conn))
    {
        var sqlQuery = @"SELECT CDU_ESTADOS  from  testetiposdestados";
        MessageBox.Show(sqlQuery);
        using(var cmd = new SqlCommand(sqlQuery, sqlConn))
        {
            using(var da = new SqlDataAdapter(cmd))
            {
                using(var table = new DataTable())
                {
                    da.Fill(table);
                    combobox1.DisplayMember = "CDU_ESTADOS";
                    comboBox1.DataSource = new BindingSource(table, null);
                }
            }
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download