Display All as Default value in ComboBox using C# and VB.Net in Windows Application

Last Reply 7 months ago By dharmendr

Posted 7 months ago

It displays blank on combobox and list all and other items. I need to display all as first item on combobox

        public void test()
        {
            DataTable dt1= new DataTable();
            dt1 = d.test(infodisplay);
            if (dt1.Rows.Count > 0)
            {
                DataRow dr = dt1.NewRow();
                dr[0] = 0;
                dr[1] = "All";
                dt1.Rows.InsertAt(dr, 0);

                cmbRack.DataSource = dt1;
                cmbRack.DisplayMember = "Rack";
                cmbRack.ValueMember="id";
                //cmbRack.SelectedIndex =0;
            }
        }

        public DataTable test(DisplayInfo dispinfo)
        {
            DataTable dt = new DataTable();
            try
            {
                if (sqlcon.State == ConnectionState.Closed)
                {
                    sqlcon.Open();
                }
                SqlCommand sccmd = new SqlCommand("Select id,Rack  from display ", sqlcon);
                

                SqlDataAdapter da = new SqlDataAdapter(sccmd);
                da.Fill(dt);
            }
            catch
            {

            }
            return dt;
        }
Posted 7 months ago

Hi gokuldas,

Refer below sample.

You nee to select only two column you can't select multiple column for that reason text is not showing in combobox.

Refer below sample code.

Namespaces

C#

using System.Data;
using System.Data.SqlClient;

VB.Net

Imports System.Data
Imports System.Data.SqlClient

Code

C#

private void Form1_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt = GetData();
    if (dt.Rows.Count > 0)
    {
        DataRow dr = dt.NewRow();
        dr[0] = 0;
        dr[1] = "All";
        dt.Rows.InsertAt(dr, 0);
        comboBox1.DataSource = dt;
        comboBox1.DisplayMember = "Country";
        comboBox1.ValueMember = "CustomerId";
    }
}

public DataTable GetData()
{
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CustomerId,Country FROM Customers", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
        }
    }
}

VB.Net

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim dt As DataTable = New DataTable()
        dt = GetData()

        If dt.Rows.Count > 0 Then
            Dim dr As DataRow = dt.NewRow()
            dr(0) = 0
            dr(1) = "All"
            dt.Rows.InsertAt(dr, 0)
            comboBox1.DataSource = dt
            comboBox1.DisplayMember = "Country"
            comboBox1.ValueMember = "CustomerId"
        End If
    End Sub

    Public Function GetData() As DataTable
        Using con As SqlConnection = New SqlConnection(constr)

            Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId,Country FROM Customers", con)

                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable()
                    da.Fill(dt)
                    Return dt
                End Using
            End Using
        End Using
    End Function

Screenshot


Posted 7 months ago
gokuldas says:
dr[0] = 0;
dr[1] = "All";

Change above code with below.

dr["id"] = 0;
dr["Rack"] = "All";