.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

How can i do TwoWay Binding between listbox and datatable

Posted By:      Posted Date: September 07, 2010    Points: 0   Category :WPF
I've bound my listbox to a datatable using the following code: Me.TasksListBox.DataContext = dt The datatemplate in the listbox is a collection of controls like textboxes and comboboxes, each of which is bound to a column in the datatable. The data loads fine in the listbox. As the users modify the data in the listbox by modifying combo boxes , textboxes of items in the listbox , i want the changes to propogate to datatable dt so i can save the values in the database. I am unable to figure out how this can be done. Thanks  

View Complete Post

More Related Resource Links

Data binding a listbox

I'm using VS 2010 and writing a WPF application.  I have a listbox on a form that I want bound to an access database field.  In the code behind I create the usual oledb connection, command and adapter and run a query.  In debug I can see the dataset has 143 items from the database, a single column called "path".  Here's the relevant code:  oleCon.Open(); adpt.SelectCommand = oleComd; adpt.Fill(dtst,"MyTable"); MusicList.DataContext = dtst; MusicList is the listbox.  There's 143 items in the dataset.  When the form loads the listbox is empty.  Here's the xaml for the listbox: <ListBox Grid.Row="1" Height="477" HorizontalAlignment="Left" Margin="7,6,0,0" Name="MusicList" VerticalAlignment="Top" Width="310" Grid.RowSpan="2" Background="#002400" ItemsSource="{Binding Path=path, Mode=OneTime}" DataContext="{Binding}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Path=path}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> At least I know the data is present but why isn't it binding to the listbox? Thanks!  

using CompositeCollection in ListBox for merging two Typed DataTable

Hi i have a Typed DataSet contining two DataTable : Table1 , Table2 i want to merge the two Tables in a ListBox by using "CompositeCollection " , but this doesn't work <ListBox.ItemsSource> <CompositeCollection> <CollectionContainer Collection="{Binding Path = Table1}"/> <CollectionContainer Collection="{Binding Path =Table2}"/> </CompositeCollection> </ListBox.ItemsSource> i put also : public partial class Window1 : Window { public Window1() { InitializeComponent(); this.FillDataset(); this.DataContext = ds; //this.Resources.Add("mytable1", DataSet1.Table1); }   i want to confess i don't have a clear idea about referencing an object as a Typed DataTable. also if a want to Template my ListBox lique this :    <ListBox.ItemTemplate> <DataTemplate DataType="{x:Type myprog:DataSet1+Table1 }"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=ProductName}" Background="AliceBlue"/> </StackPanel> </DataTemplate> <DataTemplate DataType="{x:Type myprog:DataSet1+Table2 }"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=ProductName}&quo

Binding DataTable data to ObjectDataSource

Hi all, I am trying to bind a DataTable into a ObjectDataSource. This ObjectDataSource data will be used to bind into my LocalReport in ReportViewer. I have already successfully binded my data into the DataTable. My problem now is how do i bind the data in my DataTable into the ObjectDataSource so that it can be used in my report? Here is my current codes: ASPX:<rsweb:ReportViewer ID="ReportViewer1" runat="server"> <LocalReport ReportPath="Modules\Report.rdlc"> <DataSources> <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" /> </DataSources> </LocalReport> </rsweb:ReportViewer> <asp:ObjectDataSource runat="server" ID="ObjectDataSource1"> </asp:ObjectDataSource> VB:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Try Dim dtable As DataTable = SetReportDataSource() If dtable.Rows.Count > 0 Then Dim rds As New ReportDataSource() ReportViewer1.LocalReport.DataSources.Clear() ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("Data

TwoWay Binding Datagrid To WCF Service?

Hello, i tried to bind a DataGrid in TwoWay Mode to a WCF Service. The Data get displayed but changes in DataGrid dont affect the Database, from which the WCF Service is getting the data.   I hope someone can help me... Thank you.     My Szenario: I have a WCF Service which offers exactly one Function which return a custom class. The custom class inherits from INotifyPropertyChanged and has only one DataMember, an ObservableList with my DB Entitys. //Service.svc.cs static testdatenEntities _context = null; private static testdatenEntities Get_context() { if (_context == null) { _context = new testdatenEntities(); } return _context; } public customData GetDataFromADO() { var _context = Get_context(); var result = (from lief in _context.LIEF_INFO.AsQueryable() orderby lief.LNR select lief).Skip(50).Take(50); ObservableCollection<LIEF_INFO> _ocol = new ObservableCollection<LIEF_INFO>(result); customData customdata = new customData() { List = _ocol }; return customdata; } //IService.cs [DataContract] public class customData : INotifyPropertyChanged { ObservableCollection<LIEF_INFO> _ocol = new ObservableCollection<LIEF_INFO>(); [DataMember] public ObservableCollection<LIEF_INFO> List {

Binding DataGrid to empty DataTable

Hi, I'm very much new to WPF and am finding it a bit tricky to find my way in the mass of (not always great) documentation. Right now I'm struggling to get the grid to display it's UI when bound to an empty datatable. In my XAML: <DataGrid Name="grid" ItemsSource="{Binding}" />  In my code-behind (MainWindow constructor, following InitializeComponent): var dt = new DataTable(); dt.Columns.Add("col1"); dt.Columns.Add("col2"); //var r = dt.Rows.Add("toto", "tata"); grid.DataContext = dt; If I comment out the line that adds a row to the table, all is well -- the grid generates columns and displays UI. But if the table is empty, the grid renders a thin horizontal white rectangle with no column names and it doesn't seem possible to interact with this at all. As it happens, I want the grid to be empty by default, so I need to somehow get the grid to play along. If anyone knows why this happens and what to do to solve it, I'd much appreciate the help!

Listbox - Temporary suspend Data Binding

Hi. I have a listbox bound to an ADO.NET DataTable (by his DataView) thorough ItemSource property. Whenever I add a new row to DataTable, listbox reflects that change showing an empty item. I want to late the listbox updating until row is committed to database but I can't find the right way. I tried create a Binding using BindingMode.OneTime but it doesn't work. Dim bind As New Binding bind.UpdateSourceTrigger = UpdateSourceTrigger.Explicit bind.Mode = BindingMode.OneTime bind.Source = tblRaccoltePunti.DefaultView lstRaccoltePunti.SetBinding(ListBox.ItemsSourceProperty, bind) I need a way to suspend the DataBinding to manually update listbox when row is committed.

Binding ListBox to ComboBox's SelectedItem

Hi All,   I have a ComboBox that is bound to a collection of People. When the user selects a Person in the ComboBox, I want a ListBox below that to be populated wtih the Person's Friends.   Is it something like:   <ComboBox x:Name="PeopleComboBox" ItemsSource="{Binding People}"/> <ListBox ItemsSource="{Binding ElementName=PeopleComboBox, Path=SelectedItem.Friends}"/>   I'm going for something like that.

Binding SharePoint Multiline textbox to a infopath repeating table Listbox

Hi, I have a InfoPath form  with repeating table control which gets the data from a sharepoint list. I'm trying to bind a multiline textbox to a listbox and intend to get the line by line data entered in the multiline textbox in sharepoint to show as same line by line in the form's listbox. or is there any other way of getting this done.  Thanks.

ListBox object Binding Problem...?


hii guy's

i created a ListBox and i added Engine Class object into it...so i wana display Engine Class property "Name" into ListBox..so plz tell me how can i add property name of "Name" to ListBox of Class "Engine"





Binding Datatable to listview in Xaml



  Can any one give me solution for below code.  I know how to bind the datatable to listview in code behind, but  i don't know, how to bind it through XAML. This is my sample code. Please any one help me.

<Window x:Class="ListViewDemo.MainWindow"
Title="MainWindow" Height="350" Width="525">
<DataTemplate x:Key="checkBox">
<CheckBox IsChecked="{Binding Path=Check, Mode=TwoWay}"/>
<ListView Name="lvProcess">
<GridViewColumn Header="Dep_Type" DisplayMemberBinding="{Binding Path=Dep_Type}"/>
<GridViewColumn Header="Check" DisplayMemberBinding="{Binding Path=Check}"/>
<GridViewColumn Header="Depend

update master-detail binding with a listbox and textboxes


I have a class, for example FooBar, that is databound to a Window, displaying all kinds of good stuff. It includes an ObservableCollection<Color>, for example shadesOfPink, that displays in a ListBox. This works fine, until I want to bind 3 TextBoxes to the properties ScR, ScG, and ScB of the selected Color. They can display the properties correctly, but when the user changes a value, the Color is not updated in the ListBox until the ListBox is clicked on to change the selection. I want the Color to update as soon as the TextBox loses focus. Is there a way to do this?

I have tried binding the TextBoxes to SelectedItem and binding directly to the properties but nothing worked.

I am using C# 2010.

ListBox Object Binding Problem..??


hii guy's here i'm having small problem on Object DataBinding using ListBox.. i've a WCF class like..

	public class EngineInstanceStatus 
             public string Name
				return m_name;

so i wana add dis class object to my ListBox.. den it'll display its property name "Name" in ListBox whenever i added this class object ot ListBox..

so plz tell me with sample how can i do this one..



TwoWay data binding to a specified index and property in a collection


Hello everybody,

my problem is, I can't bind to a specified item and property in a collection. The code:

    <test:ValueConverter x:Key="valueConverter" />
    <ContentPresenter x:Key="cont1" Content="{Binding Path=.[0], Mode=TwoWay}" />

ListBox.ItemsSource binding to IEnumerable



I've got a problem with binding the ListBox.ItemsSource to  a custom class implementing IEnumerable<t>.

Next is a simplified code implementing ViewModel.


 class ViewModel : System.ComponentModel.INotifyPropertyChanged
  DataCollection data = new DataCollection();

  public IEnumerable<Data> ItemsCollection // ListBox is not updated
    return data; 

  public IEnumerable<Data> ItemsCollection // Works well
    return new List<Data>(data);

 class DataCollection : IEnumerable<Data>{}

 class Data{}


This is my xaml code.


<ListBox ItemsSource="{Binding Path=ItemsCollection}"/>


If in the "ItemsCollection" property of my ViewModel I just return my object implementing IEnumerable ListBox items are not updated.

If I return new List created from my object everything works well.


ListBox ControlTemplate binding to a sub-list


There must be a simple answer to this, but I have been banging my head on this.  I am new to WPF.

Some pseudo-code:

public class MyClassA
  public string name { get; set }
  public List<string> sub-list;

and I have a ControlTemplate:


<ControlTemplate TargetType="{x:Type ListBoxItem}">

DataGridCheckBoxColumn Binding not TwoWay


I have a DataGrid on a page in my application with a DataGridCheckBoxColumn which the user can change. When the user click a button, I want get all the items in the grid for which the CheckBox is true.

What I did was create an ObservableCollection and then databind that list as follows: dgWithdrawals.DataConext = membersPenalised;

This grid is displayed in a modal popup and when the user click the accept button in the poup, I want to iterate through all the items and get all the items that are true.

Getting to the items in the grid is easy since I just need to get it again from the datacontext, but the problem is that the IsPenalised property on the object didn't change. The object does inherit from the INotifyPropertyChanged interface and I have put a breakpoint in the RaisePropertyChanged event to see if the property actually changes but it doesn't which explains why the Property is still the same when I want to get it from the DataContext of the grid.

My column definition looks like this:

<DataGridCheckBoxColumn Header="Penalised" 

Combobox inside Listbox binding issue

I am lost...

I have the following two classes (simplified)

Class Parent
    String Description{get;set;} 
    ObservableCollection<Children> ListOfChildren = new ObservableCollection<Children>();

Class Children
    public string Name{get;set;}

And the following two lists :

public List<Parent> ListOfParents = new List<Parent>();
public static List<Children> StaticListOfChildren=new List<Children>();

Now I have a list in XAML, I set the datacontext of this list to the "ListOfParents" (in code) and set the ItemSource="{Binding}"
All works fine so far. Inside this list I have another list containing the "ListOfChildren" list. The itemtemplate of this list contains a combobox. This combobox binds to the "StaticListOfChildren" list. The problem is now I can't get the selecteditem of the combobox to bind to the Children item in the ListOfChildren.... Can anyone help ??

<ListBox x:Name="lbMaterialsBuildup" HorizontalContentAlignment="Stretch" ItemsSource="{Binding}">
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend