C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

DataGrid in Dropdown using wpf


Posted Date: 05 Sep 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: LaljiMember Level: Diamond    
Rating: 1 out of 5Points: 4



This code shows how to use DataGrid in Dropdown using wpf

XAML code:

<Window x:Class="WpfComboBoxBindingTest.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="clr-namespace:WpfComboBoxBindingTest"

xmlns:s="clr-namespace:System;assembly=mscorlib"

xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"

Loaded="OnLoaded"

Title="Window1" Height="300" Width="300">

<StackPanel>

<Grid>

<ComboBox Name="comboBox"

IsEditable="False"

VerticalContentAlignment="Top"

SelectionChanged="OnSelected"

ItemsSource="{Binding Path=Tables}">

<ComboBox.ItemsPanel>

<ItemsPanelTemplate>

<VirtualizingStackPanel

Orientation="Vertical"

VerticalAlignment="Top" />

</ItemsPanelTemplate>

</ComboBox.ItemsPanel>

<ComboBox.ItemTemplate>

<DataTemplate>

<dg:DataGrid VerticalAlignment="Top"

AutoGenerateColumns="True"

ItemsSource="{Binding}" />

</DataTemplate>

</ComboBox.ItemTemplate>

</ComboBox>

</Grid>

</StackPanel>

</Window>



In the code behind:

using System.Windows;
using System.Windows.Controls;
using System.Data;

namespace WpfComboBoxBindingTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("Column1");
dt1.Columns.Add("Column2");
DataRow dr1 = dt1.NewRow();
dr1[0] = "Bob";
dr1[1] = "Smith";
dt1.Rows.Add(dr1);
DataTable dt2 = new DataTable();
dt2.Columns.Add("Column1");
dt2.Columns.Add("Column2");
DataRow dr21 = dt2.NewRow();
dr21[0] = "Jack";
dr21[1] = "Smith";
DataRow dr22 = dt2.NewRow();
dr22[0] = "Jack";
dr22[1] = "Smith";
dt2.Rows.Add(dr21);
dt2.Rows.Add(dr22);
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
comboBox.DataContext = ds;
}

private void OnSelected(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem item = comboBox.ItemContainerGenerator.ContainerFromIndex(comboBox.SelectedIndex) as ComboBoxItem;

if (item!=null)
{
//get the DataGrid and then get the selected row item
}
}
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
DataGrid in Dropdown using wpf  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Edit,Delete in Gridview
Previous Resource: How to get the value of a Hidding column in a Gridview using C#
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use