none
Bind datatable in Listbox

    General discussion

  • HI experts,

    I would like to ask how can I bind my datatable in a listbox?

    here's my sample code:

    ===============================================

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    namespace Test_3_wincontrols

    {

     public partial class Form1 : Form

        {

    public Form1()

    {

    InitializeComponent();

    // Instantiate a DataTable object

    DataTable dt = new DataTable("person");

    // DataColumn and DataRow

    DataColumn dc;

    DataRow dr;

    // create column(s)

    dc = new DataColumn();

    dc.DataType = System.Type.GetType("System.String");

    dc.ColumnName = "Name";

    dt.Columns.Add(dc);

    dc = new DataColumn();

    dc.DataType = System.Type.GetType("System.DateTime");

    dc.ColumnName = "Bdate";

    dc.ReadOnly = true;

    dt.Columns.Add(dc);

    //Add Datarow

    dr = dt.NewRow();

    dr["Name"] = "Tatas";

    dt.Rows.Add(dr);

    dr = dt.NewRow();

    dr["Bdate"] = "09/24/2008";

    dt.Rows.Add(dr);

    }

    }

    }


    tatas move
    • Edited by cerebralkink Wednesday, September 24, 2008 6:59 AM
    Wednesday, September 24, 2008 6:57 AM

All replies

  • Something like:

    listBox1.DataSource = dt;
    listBox1.DataMember = "person";
    listBox1.DisplayMember = "Name";
    Thursday, September 25, 2008 7:31 AM