Solved

Treeview Bind datatable to Checkbox - How to

Posted on 2010-11-15
2
1,304 Views
Last Modified: 2013-11-12
I have a datatable that looks like   NAME,  ISseelcted
I need to create a TREEView with a CHECKBOX and bind it to the datatable...
thanks
0
Comment
Question by:JElster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 34142816
sample below
XAML

<TreeView ItemsSource="{Binding Table.DefaultView}">
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <StackPanel >
                        <TextBlock Text="{Binding Row[0]}"/>
                        <TextBlock Text="{Binding Row[1]}"/>
                    </StackPanel>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>


code behind

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.DataContext = this;
            DataRowView view;
            
        }

        public DataTable Table { get; set; }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            Table = new DataTable();
            
            DataColumn cl = new DataColumn("Col1", typeof(string));
            cl.MaxLength = 100;
            Table.Columns.Add(cl);

            cl = new DataColumn("Col2", typeof(string));
            cl.MaxLength = 100;
            Table.Columns.Add(cl);

            DataRow rw = Table.NewRow();
            Table.Rows.Add(rw);
            rw["Col1"] = "Value1";
            rw["Col2"] = "Value2";
        }
    }

Open in new window

0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 250 total points
ID: 34142819
with check box
<Window x:Class="TestTableBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <TreeView ItemsSource="{Binding Table.DefaultView}">
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <StackPanel >
                        <TextBlock Text="{Binding Row[0]}"/>
                        <CheckBox IsChecked="{Binding Row[1]}"/>
                    </StackPanel>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>


public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.DataContext = this;
            DataRowView view;
            
        }

        public DataTable Table { get; set; }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            Table = new DataTable();
            
            DataColumn cl = new DataColumn("Col1", typeof(string));
            cl.MaxLength = 100;
            Table.Columns.Add(cl);

            cl = new DataColumn("Col2", typeof(string));
            cl.MaxLength = 100;
            Table.Columns.Add(cl);

            DataRow rw = Table.NewRow();
            Table.Rows.Add(rw);
            rw["Col1"] = "true value";
            rw["Col2"] = true;

            rw = Table.NewRow();
            Table.Rows.Add(rw);
            rw["Col1"] = "false value";
            rw["Col2"] = false;
        }
    }

Open in new window

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question