Page

Bind a Windows Forms ComboBox or ListBox Control to Data

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;


namespace VehicleDetails.Report
{
    public partial class LoanForm : Form
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcon"].ConnectionString);
        public LoanForm()
        {
            InitializeComponent();
            bind();

        }

      private void bind()
         {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select ID,Veh_Number from Vehicle_Detail where Pur_Loan='Yes'", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            DataRow dr;
            dr = dt.NewRow();
            dr.ItemArray = new object[] { 0, "-----Select Vehicle Number-----" };
            dt.Rows.InsertAt(dr, 0);
            comboBox1.DisplayMember = "Veh_Number";
            comboBox1.ValueMember = "ID";
            comboBox1.DataSource = dt;
            con.Close();
        }
}

No comments:

Post a Comment