Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

ComboBox + DataBinding and display values

$
0
0

Hello,

As part of a larger project I have a tblPickList with 3 columns, cboPickListName, cboPickListDescription & cboPickListTable. I managed to get the ComboBox to display the contects of cboPickListName.

As I am a total newbie and follow the advice given here i am stuck on something totally stupid.

I would like to display the content of the selected cboPickListName, cboPickListDescription & cboPickListTable in order to use the latter to open a datagrid view of the relevant table.

What have i missed?

//
// Combining ComboBox with data table to open the relevant table
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace ComboData
{
    public partial class frmPickList : Form
    {
        //Sql Connection declared
        SqlConnection connection; // required

        //Declare connection string
        String connectionString;    // required
        //private object tblPickListTableAdapter;

        public frmPickList()
        {
            InitializeComponent();
            // connect to the data with this string
            connectionString = ConfigurationManager.ConnectionStrings["ComboData.Properties.Settings.PicklistConnectionString"].ConnectionString;  // the string is found in App.config
        }

        private void FrmPickList_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'picklistDataSet.tblPickList' table. You can move, or remove it, as needed.
            //this.tblPickListTableAdapter.Fill(this.picklistDataSet.tblPickList);
            //Define Calls
            PopulatePickList();     // required - names the sequence 
        }

        private void PopulatePickList()     // required
        {
            // by using () Sql will close automatically or else you must add connection.Close();
            using (connection = new SqlConnection(connectionString))
            using (SqlDataAdapter adapter = new SqlDataAdapter("Select * FROM tblPickList", connection))
            {
                DataTable tblpicklistTable = new DataTable();
                adapter.Fill(tblpicklistTable);

                //Insert the Default Item to DataTable.
                DataRow row = tblpicklistTable.NewRow();
                row[0] = 0;
                row[1] = "Please select";
                tblpicklistTable.Rows.InsertAt(row, 0);

                // set list box properties
                cboPickList.DisplayMember = "cboPickListName";
                cboPickList.ValueMember = "Id";
                //Hook-up listbox to the data
                cboPickList.DataSource = tblpicklistTable;
            }
        }

        // to display the selection
        // this is where you will open the data table
        private void cboPicklist_Select(object sender, EventArgs e)
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("Select * FROM tblPickList WHERE cboPickListName = cboPickList", connection))
            lblPickListValue.Text = cboPickList.Text;lblPickListDescription.Text = lblPickListDescription.Text; // this does not work
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>