Skip to content

Multi Column ComboBox but when Selecte Value should be Id

Ifraz Imanudeen

Asked Sep 7, 2018, 4:27 PM EDT1 answer
Share
LinkedInX
Hi everyone,
 
i have the done following coding, unable to get the it working..
 
i have a Table with Id, Product, FolderPath.
 
i have a comboBox2 where i need two column of data from above table "Id" & "Product" to been shown in comboBox2. Now if i select a Row from comboBox2 i need the textBox1 and textBox2 to be filled with "Product" and "FolderPath" based on the comboBox2 selection.
 
Like even though the comboBox2 shows  "Id" & "Product" internally the value has to be "Id".
 
Following are my code; can someone help me on this.
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using System.Drawing;  
  7. using System.Linq;  
  8. using System.Text;  
  9. using System.Threading.Tasks;  
  10. using System.Windows.Forms;  
  11. using System.IO;  
  12.   
  13.    
  14.   
  15. namespace WindowsFormsApp1  
  16. {  
  17.  public partial class Main : Form  
  18.  {  
  19.  string connString;  
  20.  SqlConnection conn;  
  21.  public Main()  
  22.  {  
  23.  InitializeComponent();  
  24.  }  
  25.   
  26.   
  27.   
  28. private void Main_Load(object sender, EventArgs e)  
  29.  {  
  30.  connString = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=ifz001;";  
  31.  conn = new SqlConnection(connString);  
  32.  conn.Open();  
  33.  Load_Products();  
  34.  }  
  35.    
  36.    
  37.   
  38. void Load_Products()  
  39.  {  
  40.  try  
  41.  {  
  42.  SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FileFolderPath", conn);  
  43.  DataTable dt = new DataTable();  
  44.  da.Fill(dt);  
  45.   
  46.   
  47.   
  48. for(int i = 0; i < dt.Rows.Count; i++)  
  49.  {  
  50.  comboBox2.Items.Add(dt.Rows[i]["Id"].ToString() + "  |  " + dt.Rows[i]["Product"].ToString());  
  51.   
  52.   
  53.   
  54.   
  55. comboBox2.ValueMember = dt.Rows[i]["Id"].ToString();  
  56.  }  
  57.   
  58.   
  59.   
  60.   
  61. }  
  62.  catch (Exception lp)  
  63.  {  
  64.  MessageBox.Show(lp.Message);  
  65.  }  
  66.  }  
  67.   
  68.    
  69.   
  70. private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)  
  71.  {  
  72.  /*try 
  73.  {*/  
  74.   
  75.  var pix = "SELECT * FROM FileFolderPath WHERE Id = '" + comboBox2.ValueMember + "'";  
  76.  SqlCommand cmd = new SqlCommand(pix, conn);  
  77.  cmd.ExecuteNonQuery();  
  78.  DataTable dt = new DataTable();  
  79.  SqlDataAdapter da = new SqlDataAdapter(cmd);  
  80.  da.Fill(dt);  
  81.  foreach(DataRow dr in dt.Rows)  
  82.  {  
  83.   
  84.  PID.Text = dr["Product"].ToString();  
  85.  PPath.Text = dr["FolderPath"].ToString();  
  86.   
  87.  }  
  88.   
  89. /*} 
  90.  catch (Exception ex) 
  91.  { 
  92.  MessageBox.Show(ex.Message); 
  93.  }*/  
  94.  }  
  95.   
  96.    
  97.   
  98.    

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Acheived using below changed codes:
     
    1. void Load_Products()  
    2.        {  
    3.            try  
    4.            {  
    5.                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FileFolderPath", conn);  
    6.                DataTable dt = new DataTable();  
    7.                da.Fill(dt);  
    8.   
    9.                for(int i = 0; i < dt.Rows.Count; i++)  
    10.                {  
    11.                    string pid = dt.Rows[i]["Id"].ToString() as string;  
    12.                    string p = dt.Rows[i]["Product"].ToString();  
    13.   
    14.                    comboBox2.Items.Add(p);  
    15.                    comboBox2.DisplayMember = p;  
    16.                    comboBox2.ValueMember = pid.ToString() as string;  
    17.                }  
    18.   
    19.            }  
    20.            catch (Exception lp)  
    21.            {  
    22.                MessageBox.Show(lp.Message);  
    23.            }  
    24.        }  
    25.   
    26.        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)  
    27.        {  
    28.            try  
    29.            {  
    30.   
    31.                var ppat = "SELECT * FROM FileFolderPath WHERE Id = '" + comboBox2.SelectedIndex + "' ++1";  
    32.                SqlCommand cmd = new SqlCommand(ppat, conn);  
    33.                cmd.ExecuteNonQuery();  
    34.                DataTable dt = new DataTable();  
    35.                SqlDataAdapter da = new SqlDataAdapter(cmd);  
    36.                da.Fill(dt);  
    37.                foreach(DataRow dr in dt.Rows)  
    38.                {  
    39.                      
    40.                    PID.Text = dr["Id"].ToString();  
    41.                    PPath.Text = dr["FolderPath"].ToString();  
    42.                     
    43.                }  
    44.            }  
    45.            catch (Exception ex)  
    46.            {  
    47.                MessageBox.Show(ex.Message);  
    48.            }  
    49.        }  
     
    0