populate listbox with data table in winforms

Posted by Shanky11 under Windows Forms on 9/30/2013 | Points: 10 | Views : 1827 | Status : [Member] | Replies : 3
ihave to use a list box and populate it with dynamic dtatable
how an i do this???




Responses

Posted by: Bandi on: 9/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://www.daniweb.com/software-development/csharp/threads/190555/populate-listbox-with-an-sql-query
http://forums.asp.net/t/1934372.aspx


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
public mainForm()
{
InitializeComponent();
SqlConnection conn = new SqlConnection("Data Source=DBELL;Initial Catalog=part_table;Integrated Security=True");
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT part_num from customParts", conn);
adapter.Fill(ds);
this.listParts.DataSource = ds.Tables[0];
this.listParts.DisplayMember = "part_num";
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
SqlConnection _connection = new Connection(connectionString)

SqlDataAdapter _adapter = new SqlDataAdapter(_connection, "select * from tag")
DataTable _table = new DataTable()
_adapter.Fill(_table)

_connection.Close();

foreach(DataRow _row in _table.Rows)
{
listbox.AddItem(new Item(_row["column1"], _row["column2"])
}

You don't need to mess with datatables. It automatically binds with the sql query.

Reference: http://stackoverflow.com/questions/114851/how-do-i-bind-the-result-of-datatable-select-to-a-listbox-control

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response