Ifraz Imanudeen

Ifraz Imanudeen

  • NA
  • 60
  • 3.1k

Multi Column ComboBox but when Selecte Value should be Id

Sep 7 2018 8:27 PM
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.    
 
 

Answers (1)