hi, developing a windows application using visual basic 2008. in a form, i want to load customer ID and customer Name in a combo boX. customer ID will be invisible and customer name will be visible in column.user will select the name but customer id will be stored in the database.
Thanx
Loading
vijayPosted May 19, 2011, 8:26 AM
Bryian TanPosted May 3, 2011, 1:11 AM
Yes, we can have more than 2 columns/entity/column store in the ComboBox. Here is a brief example.
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;
namespace WindowsFormsSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
PopulateComboBox();
}
public void PopulateComboBox()
{
List
new Book(1, "Title 1", "Author 1", "Category 1", "9992158107"),
new Book(2, "Title 2", "Author 2", "Category 2", "xyz5345508"),
new Book(3, "Title 3", "Author 3", "Category 3", "8777158107"),
new Book(4, "Title 4", "Author 4", "Category 4", "abc5555107"),
new Book(5, "Title 5", "Author 5", "Category 5", "9323443507"),
new Book(6, "Title 6", "Author 6", "Category 6", "9534553455"),
new Book(7, "Title 7", "Author 7", "Category 7", "9434543107"),
new Book(8, "Title 8", "Author 8", "Category 8", "998758107")};
comboBox1.DataSource = books;
comboBox1.DisplayMember = "Title";
comboBox1.ValueMember = "ID";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Book b = (Book)comboBox1.SelectedItem;
MessageBox.Show("Author " + b.Author + "\r\n Title: " + b.Title +
"\r\n Category: " + b.Category + "\r\n ISBN: " + b.ISBN);
}
}
public class Book
{
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Category { get; set; }
public string ISBN { get; set; }
public Book(int id, string title, string author, string category, string isbn)
{
ID = id;
Title = title;
Author = author;
Category = category;
ISBN = isbn;
}
}
}
Ankit NandekarPosted May 3, 2011, 12:46 AM
mazez shaPosted May 2, 2011, 11:53 PM
Ankit NandekarPosted May 2, 2011, 6:04 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=C3;Initial Catalog=Student;Persist Security Info=True;User ID=sa;Password=p@ssw0rd");
SqlDataAdapter da = new SqlDataAdapter("select EmpName, Empno from EmployeeInfo", con);
DataSet ds = new DataSet();
da.Fill(ds);
comboBox1.DataSource = ds.Tables[0].DefaultView;
comboBox1.DisplayMember = "EmpName";//Employee name which u want to show in Combobox
comboBox1.ValueMember = "Empno";
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString()); //u can get selected employee's id using this code
}
}
}
in above example it is reteriving Employee name and Employee id from table and with the use of comboBox1.SelectedValue u can get Employee Id for perticular Employee.
VulpesPosted May 2, 2011, 5:26 AM