C# .NET - How to bind data with ComboBox? - Asked By Eswaran Radhakrishnan on 12-Aug-08 06:46 AM

hi all,

I tried to bind datas with comboBox in windows application. But I am getting only "System.Data.DataRow" in my comboBox.

I used the following code

ComboBox1..DataSource = ds.Tables["pop"].DefaultView;
ComboBox1.DisplayMember = "partyId";

But I am getting "System.data.DataRow" in my comboBox.

So,How to bind data with comboBox?

Thanks
R. eswaran

use this - Web Star replied to Eswaran Radhakrishnan on 12-Aug-08 06:49 AM

Binding to a Combobox

The Combobox doesn't have a DataMember property - instead it has a DisplayMember and ValueMember property:

  • The DisplayMember of a Combobox gets or sets a string that specifies the property of the data source whose contents you want to display.
  • The ValueMember property determines which value gets moved into the SelectedValue of the Combo Box. In the example, whenever the user selects a Customer by "Customers.CompanyName", the SelectedValue is the "Customers.CustomerID". Whenever the SelectedValue changes, the data-binding moves the new value into the Customer object.

// Setup the combobox view and display-, value member
cbCust.DataSource = dsView;
cbCust.DisplayMember = "Customers.CompanyName";
cbCust.ValueMember = "Customers.CustomerID";

also see this linke

http://www.akadia.com/services/dotnet_databinding.html

Use this... - Atul Shinde replied to Eswaran Radhakrishnan on 12-Aug-08 06:54 AM

combobox1.DataSource  = ds;

combobox1.DisplayMember = "EmpName";

combobox1.ValueMember = "EmpId";

See this - san san replied to Eswaran Radhakrishnan on 12-Aug-08 06:58 AM

Hi
try this example..

// Establish the Relationship "RelCustOrd" between Customers ---< Orders

System.Data.DataRelation relCustOrd;
System.Data.DataColumn colMaster1;
System.Data.DataColumn colDetail1;
colMaster1 = ds.Tables["Customers"].Columns["CustomerID"];
colDetail1 = ds.Tables["Orders"].Columns["CustomerID"];
relCustOrd = new System.Data.DataRelation("RelCustOrd",colMaster1,colDetail1);
ds.Relations.Add(relCustOrd);

Binding to a Combobox

The Combobox doesn't have a DataMember property - instead it has a DisplayMember and ValueMember property:

  • The DisplayMember of a Combobox gets or sets a string that specifies the property of the data source whose contents you want to display.
  • The ValueMember property determines which value gets moved into the SelectedValue of the Combo Box. In the example, whenever the user selects a Customer by "Customers.CompanyName", the SelectedValue is the "Customers.CustomerID". Whenever the SelectedValue changes, the data-binding moves the new value into the Customer object.

// Setup the combobox view and display-, value member
cbCust.DataSource = dsView;
cbCust.DisplayMember = "Customers.CompanyName";
cbCust.ValueMember = "Customers.CustomerID";

Hope this helps
SAN
Bind data with Combobox - SP replied to Eswaran Radhakrishnan on 12-Aug-08 06:59 AM
Dim str As String
        str = "Select * from CountryTable"
        ddCountry.DataSource = obj.GetDateSet(str)
        ddCountry.Items.Clear()
        ddCountry.DataValueField = "COUNTRYID"
        ddCountry.DataTextField = "COUNTRYName"
        ddCountry.DataBind()

//GetDateSet is a function which returns a dataset.
How to bind data with ComboBox? - ajay choudhary replied to Eswaran Radhakrishnan on 12-Aug-08 07:04 AM
select ID,Name from TableName ;
dr DBDatareader = "query"
li ListItem = null;
check dr.read()
{
  li = new ListItem(dr["Name"],dr["id"]);
  cmb.items.Add(li);
}

Use this logic... - Atul Shinde replied to Eswaran Radhakrishnan on 12-Aug-08 07:19 AM

Code:

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ATUL\WebSite\TryOutWebsite\App_Data\Database.mdf;Integrated Security=True;User Instance=True");

SqlCommand cmd = null;

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

cmd = new SqlCommand();

cmd.Connection = conn;

cmd.CommandType = CommandType.Text;

cmd.CommandText = "Select * from EMP";

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

conn.Open();

da.Fill(ds);

conn.Close();

GridView gv = new GridView();

gv.DataSource = ds;

gv.DataBind();

DropDownList1.DataSource = ds;

DropDownList1.DataTextField = "EmpName";

DropDownList1.DataValueField = "EmpId";

DropDownList1.DataBind();

}

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

Label1.Text = Convert.ToString(DropDownList1.SelectedValue);

}

Database Data Binding: ComboBox - Binny ch replied to Eswaran Radhakrishnan on 12-Aug-08 12:31 PM

see this example:

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources

Public Class MainClass
    Shared Sub Main()
        Dim form1 As Form = New Form1()
        Application.Run(form1)

    End Sub
End Class



Public Class Form1
    'Form level variables
    Private strConnectionString As String = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=Employee.mdb;"

    Private objConnection As OleDbConnection
    Private objCommand As OleDbCommand
    Private objDataAdapter As OleDbDataAdapter
    Private objDataTable As DataTable

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load

        'Initialize the Connection object
        objConnection = New OleDbConnection(strConnectionString)

        'Initialize the Command object
        objCommand = New OleDbCommand("SELECT ID, FirstName " & _
            "FROM Employee", objConnection)

        'Initialize the DataAdapter object and set the SelectCommand property
        objDataAdapter = New OleDbDataAdapter
        objDataAdapter.SelectCommand = objCommand

        'Initialize the DataTable object
        objDataTable = New DataTable

        'Populate the DataTable
        objDataAdapter.Fill(objDataTable)

        'Bind the DataTable to the ComboBox
        ComboBox1.DataSource = objDataTable
        ComboBox1.DisplayMember = "FirstName"
        ComboBox1.ValueMember = "ID"

        'Clean up
        objDataAdapter.Dispose()
        objDataAdapter = Nothing
        objCommand.Dispose()
        objCommand = Nothing
        objConnection.Dispose()
        objConnection = Nothing
    End Sub
End Class

Go through this link:

http://www.java2s.com/Code/VB/Database-ADO.net/DatabaseDataBindingComboBox.htm

How to: Bind a Windows Forms ComboBox Control to Data - Binny ch replied to Eswaran Radhakrishnan on 12-Aug-08 12:34 PM

To bind a ComboBox or ListBox control

  1. Set the DataSource property to a data source object. Possible data sources include a http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx bound to data, a data table, a data view, a dataset, a data view manager, an array, or any class that implements the http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx interface. For more information, see http://msdn.microsoft.com/en-us/library/f3y6cb0c.aspx.

  2. If you are binding to a table, set the DisplayMember property to the name of a column in the data source.

    - or -

    If you are binding to an http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx, set the display member to a public property of the type in the list.                                        

  3. Private void BindComboBox()
    {
      comboBox1.DataSource = dataSet1.Tables["Suppliers"];
      comboBox1.DisplayMember = "ProductName";
    }

Use this - Atul Shinde replied to Eswaran Radhakrishnan on 13-Aug-08 02:08 AM

The Combobox doesn't have a DataMember property - instead it has a DisplayMember and ValueMember property:

  • The DisplayMember of a Combobox gets or sets a string that specifies the property of the data source whose contents you want to display.
  • The ValueMember property determines which value gets moved into the SelectedValue of the Combo Box. In the example, whenever the user selects a Customer by "Customers.CompanyName", the SelectedValue is the "Customers.CustomerID". Whenever the SelectedValue changes, the data-binding moves the new value into the Customer object.

// Setup the combobox view and display-, value member
cbCust.DataSource = dsView;
cbCust.DisplayMember = "Customers.CompanyName";
cbCust.ValueMember = "Customers.CustomerID";

also see this linke

go through - selva murugan replied to Eswaran Radhakrishnan on 14-Aug-08 07:22 AM

ComboBox1.DataSource = dsUserRole.Tables[0];

ComboBox1.DataValueField = "rol_id";

ComboBox1.DataTextField = "rol_descr";

ComboBox1.DataBind();

ComboBox1.Items.Insert(0,new ListItem("Select", "0"));

Using datasource property of combobox - Prakash Panchal replied to Eswaran Radhakrishnan on 03-Apr-09 02:35 AM

This is not the correct method, correct method is as follow,

cmbDate.DisplayMember = "BATCHDATE";  --------> column name for which data should be displayed in conbobox

cmbDate.ValueMember = "BATCHDATE"; 

 cmbDate.DataSource = dt_Global; -->data table name


SVK N replied to Binny ch on 26-Oct-10 09:12 AM
 
da.fill(ds,"tablename")
comboBox1.datasource=ds
ComboBox1.DisplayMember = 
"FirstName"
    ComboBox1.ValueMember = "ID"

i am using the above code but the combobox is displayed empty
no records gets populated
supriya replied to Eswaran Radhakrishnan on 29-Apr-11 02:20 AM
hi
sujeet replied to Eswaran Radhakrishnan on 23-Jun-11 05:48 AM
  
       Object SelectedComboData=ComboBox.selectedItem;
         Datarow row=((DatarowView)SelectedComboData).Row;
       String data=row[0].toString();
sujeet replied to Eswaran Radhakrishnan on 23-Jun-11 05:50 AM
  Object SelectedComboData=ComboBox.selectedItem;
         Datarow row=((DatarowView)SelectedComboData).Row;
       String data=row[0].toString();
sree sony replied to selva murugan on 29-May-12 09:18 AM
ComboBox1.Items.Insert(0,new ListItem("Select", "0"));  its not working in C#