ComboBox2ListBox
May 27th 2008 04:04
C# code for selecting ComboBox item into ListBox:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
namespace WpfApplication1_grid
{
/// <summary>
/// Interaction logic for combo2List.xaml
/// </summary>
public partial class combo2List : System.Windows.Window
{
public combo2List()
{
InitializeComponent();
comboBox1.SelectionChanged = new SelectionChangedEventHandler( comboBox1_SelectionChanged);
}
void comboBox1_SelectionChanged(ob ject sender, SelectionChangedEventArgs e)
{
//throw new NotImplementedException();
string strItem = "";
System.Data.DataRowView dr = (System.Data.DataRowView)comb oBox1.SelectedItem;
strItem = dr.Row["firstname"].ToString();
listBox1.Items.Add(strItem);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ListCat();
}
private void ListCat()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("Data source=cp960sw;initial catalog=VH-ENH-POC;uid=sa;pas sword=rmsindia;");
SqlDataAdapter da = new SqlDataAdapter("Select * from student", con);
da.Fill(ds);
comboBox1.DataContext = ds.Tables[0].DefaultView;
comboBox1.DisplayMemberPath = ds.Tables[0].Columns[1].ToString();
comboBox1.SelectedValuePath = ds.Tables[0].Columns[0].ToString();
}
}
}
XAML code for selecting ComboBox item into Listbox:
<Window x:Class="WpfApplication1_grid .combo2List"
Really Long Link
Really Long Link
Title="combo2List" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<ComboBox Height="23" Margin="104,43,54,0" ItemsSource="{Binding}" Name="comboBox1" VerticalAlignment="Top" IsSynchronizedWithCurrentItem ="False"></ComboBox>
<ListBox Margin="102,121,56,41" Name="listBox1" IsSynchronizedWithCurrentItem ="False" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
namespace WpfApplication1_grid
{
/// <summary>
/// Interaction logic for combo2List.xaml
public partial class combo2List : System.Windows.Window
{
public combo2List()
{
InitializeComponent();
comboBox1.SelectionChanged = new SelectionChangedEventHandler( comboBox1_SelectionChanged);
}
void comboBox1_SelectionChanged(ob ject sender, SelectionChangedEventArgs e)
{
//throw new NotImplementedException();
string strItem = "";
System.Data.DataRowView dr = (System.Data.DataRowView)comb oBox1.SelectedItem;
strItem = dr.Row["firstname"].ToString();
listBox1.Items.Add(strItem);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ListCat();
}
private void ListCat()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("Data source=cp960sw;initial catalog=VH-ENH-POC;uid=sa;pas sword=rmsindia;");
SqlDataAdapter da = new SqlDataAdapter("Select * from student", con);
comboBox1.DataContext = ds.Tables[0].DefaultView;
comboBox1.DisplayMemberPath = ds.Tables[0].Columns[1].ToString();
comboBox1.SelectedValuePath = ds.Tables[0].Columns[0].ToString();
}
}
}
XAML code for selecting ComboBox item into Listbox:
<Window x:Class="WpfApplication1_grid .combo2List"
Really Long Link
Really Long Link
Title="combo2List" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<ComboBox Height="23" Margin="104,43,54,0" ItemsSource="{Binding}" Name="comboBox1" VerticalAlignment="Top" IsSynchronizedWithCurrentItem ="False"></ComboBox>
<ListBox Margin="102,121,56,41" Name="listBox1" IsSynchronizedWithCurrentItem ="False" />
</Grid>
</Window>
| 20 |
| Vote |
Subscribe to this blog





