If I select a value from the first combo box, that value should not be listed in the second combo box.
ComboBox1 ComboBox2 One One Two Two Three Three Four Four Five Five
If I select 'Three' from the ComboBox1, the value 'Three' should not be listed in ComboBox2 while making a selection from ComboBox2.
Satyapriya NayakPosted Jan 8, 2012, 5:16 AM
Better use DataReader instead of dataset.
Then the code pravin suggested will work.
Try this...
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.Data.OleDb;
namespace Datagridview_bind
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oda;
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
additems();
}
void additems()
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from tablename";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["name"]);
comboBox2.Items.Add(reader["name"]);
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string value1 = comboBox1.Text;
string value2 = "";
for (int i = 0; i < comboBox2.Items.Count; i++)
{
value2 = comboBox2.Items[i].ToString();
if (value1 == value2)
{
comboBox2.Items.RemoveAt(i);
}
}
}
}
}
Thanks
najir mPosted Jan 8, 2012, 3:15 AM
With the above code once I select "One" from the combobox1, the same value is again displayed in combobox2.
With this code
value2 = comboBox2.Items[i].ToString();
value2 variable is not getting any values from combobox2.
Pravin GhadgePosted Jan 8, 2012, 12:36 AM
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string value1 = comboBox1.Text;
string value2 = "";
//comboBox2.Items.r
for (int i = 0; i < comboBox2.Items.Count; i++)
{
value2 = comboBox2.Items[i].ToString();
if (value1 == value2)
{
comboBox2.Items.RemoveAt(i);
}
}
}