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

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

DataGrid Binding

Posted By:      Posted Date: October 14, 2010    Points: 0   Category :WPF


I have databound an observableCollection to my datagrid. The objects in the observableCollection contains a "ProjectStarted" property of type "DateTime". Currently the property is displayed as a full date and time string, but i would like to call the dateTime function "ToShortDateString()" before displaing the date. Is this possible?


Best regards Peter

View Complete Post

More Related Resource Links

Data Binding a DataGrid to a list fetch the same data multiple times

Hi, I'm using DataGrid to show data through data binding. My data are provided in a list. The setup I have is the following: DataGrid "binded" to the default view of my list that implement IList<T> and IList interfaces (MyList<T> : IList<T>, IList, INotifyCollectionChanged)             CollectionViewSource.GetDefaultView(_myList);             // Set the data context             DataContext = view; T: is here my 'business object' that hold the data, called 'MyRow'. Then the columns of the DataGrid are bound to public properties of 'MyRow' object. MyRow class has a public property of type 'object' (public object data[int index]) and first column of datagrid is bound to myRow.data[0], second column is bound to myRow.data[1] and so on.   When the datagrid is displayed, databinding kick in and my data are showed in the control. The issue I'm having is that data fetching is very repetitive even for the same rows. For example for a datagrid with 20 visible rows, the first row is fetched multiple times even if the datagrid is not resized or moved. I put break point inside my list (MyList<T> into This[T] property (public T this[int index]) and i got multiple hits into the property for the same index. Fetc

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!

Binding DataGrid RowDetailTemplate Using MVVM

In this post I will show you how you can bind the RowDetailTemplate of the data grid using the MVVM pattern. Below are the image of the sample code here you can see the default page in Image 1 and when user click on any of the record then detail of the record is shown in the RowDetailTemplate which also consist of the data grid control which you can see in the Image 2.

Click.CommandParameter null when binding on Button inside DataGrid DataGridTemplateColumn

I am having trouble getting a Click.CommandParameter to pass through with a bound Click.Command. I have a DataGrid with a DataGridTemplateColumn that contains a couple of Buttons. Using a suggestion found on the forum (http://social.msdn.microsoft.com/Forums/en/wpf/thread/541024a9-3d7e-4c6a-b88e-9a7eb7ee83a9), I was able to get the Click.Command to bind to the right command, but when arriving in the corresponding method in my ViewModel, the parameter is null.  Here is my xaml for my DataGridTemplateColumn <data:DataGridTemplateColumn Header="Case">  <data:DataGridTemplateColumn.CellTemplate>   <DataTemplate >    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">     <Button Content="Open" Margin="6,0" command:Click.Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type data:DataGrid}}, Path=DataContext.OpenCaseCommand}" CommandParameter="{Binding}"  />     <Path Data="M1,1 L1,2" Stroke="#FFCCCCCC"/>     <Button Content="Rename" />    </StackPanel>   </DataTemplate>  </data:DataGridTemplateColumn.CellTemplate> </data:DataGridTemplateColumn> I used Snoop to see what was going on. Everything

Binding a computed value to datagrid column

Hi, I have a WPF Datagrid with tree colums (A, B,C) which are bound to an Observable collection. The value of C is C=A+B. The value of A and B are updated via an event that I have subscribed to ( a realtime data feed). What is the best way to update the value of C each and every time A or B get chaged ? I'm new to WPF and should I use a converter to achive this ? Thanks V

Adding new rows to Datagrid with a Combobox column generate "two-way binding requires path or xpath"



    I have datagrid control with a template combobox column like:

 <DataGridTemplateColumn Header="Fault code" Width="75">
                                    <TextBlock Text="{Binding FaultCode1}" />

Dynamically Assign Datagrid Column Binding from XAML


I am trying to build a generic user control that will serve as a quick list maintenance screen.

I have a base ViewModel that has two string properties IDColumnName and DisplayColumnName and then a collection of objects to be edited.  Inherited view models will be created for each object type that needs to be maintained.  The usercontrols datacontext is set to the viewmodel.

What I want to do is have the datagrid, who's itemssource is bound to the collection of objects in the viewmodel, have one column and have that one column bound to the property specified in the DisplayColumnName property of the viewmodel, and I'd like to do it all in XAML.  I've got the datagrids rowdetailstemplate being dynamically determined based on the object type but am struggling with dynamically setting which property the one column displays.

Dynamicobject collection binding on WPF DataGrid , not working ?



I am trying to bind dynamic list of items to the WPF DataGrid , but its not working .

my Dynamic list is

 // CustomDynamicObj is inherited from DynamicObject

  dynamic listItems = new List<CustomDynamicObj>();

 this.dataGrid1.DataContext = listItems;

And my XAML is

   <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="dataGrid1" VerticalAlignment="Stretch"  />

If I add columns through code in DataGrid then above code works fine , but I want to Autogenerate columns .

Any workaround for this plz ?



WPF Datagrid binding as a subset of another Binding, does not update on new row



I have created 2 instances of this semi complex senario (similar to my previous post).  To start off, I have a many to many database relationship between 2 tables, so I created a linking table inbetween.  Take a dataset, make 3 tables, create a relationship from one table to another to the other.  So table A is has a many to many relationship to table B via table C (which simply links Aid, to Bid in a bunch of rows).  Take a WPF window, make 2 datagrids, one based on table A, and one based on the relationship table A has to table B.  (bind through the relationship not directly to the source table).  When you select a value in table A, table B's related values appear, and not the whole table B.  Got that far?

Datagrid B, for the relationship, is going to be bizzare...  Make a custom datagrid, add 1 column (a datagridtemplate column).  In the CellEditingTemplate, add a single combobox, with ALL of Table B as an ItemsSource. (we now should have 4 CollectionViewSources, All of Table A, Related A to C, Related C to B, All of Table B).  This can be done simply by using the DataSource window and opening table A, then open table C (inside of table A), then open table B (inside of table C, inside of table A), then drag table B into the window.  WPF will create all

Binding itemsource for 1 combo box column from another combo box column in a datagrid


Have a hierarchical list of data that the user can select values from, however they have to select the second item from a list that is restricted based on the entry selected in the first combo box.


Item a

  • item 1
  • item 2
  • item 3

Item b

  • item 1
  • item 4
  • item 5
  • item 6

Item C

  • item 2
  • item 3
  • item 5
  • item 6

Does anyone have any ideas about how to go about populating the first and second combo boxes so that this would work?

WPF: Binding Dictionary to DataGrid and making it editable.



Basically, I would like to make  "Property Editor" using Dictionary and DataGrid by storeing "properties" (key&value pairs) into Dictionary and DataGrid to display and edit. 

I was able to successfully bind using below code.

It displays Key&Value pairs fine in the DataGrid but it won't allow me to edit the Value.

It says, "EditItem is not allowed for this view"

How can I do this using DataGrid and Dictionary?

Is there better way to make property editor with FLEXIBLE number of properties?

I emphasized "flexible" because the property entries/definition can frequently change and I thought Dictionary was perfect for this.

Thanks a lot!


 <DataGrid Name="PropertyGrid"
      				<DataGridTextColumn Header="Name"
 Binding="{Binding Path=Key}"
      				<DataGridTextColumn Header="Valu

DataGrid binding very slow while listview is pretty fast...


I want to bind DataGrid to a DataTable, but the binding is very slow.

actually, I'm not sure if it's the binding itself, or the DataGrid "drawing" on the screen (I'm trying to bind 300 rows with 27 columns).

I tried to replace the DataGrid with listview and it's working just fine.

I can't use the listView since i want the rows to be editable, is it known issue? is there any solution?



EF Binding to DataGrid at design time in VS2010


I'm playing arround with the DataGrid for wpf and EF in Visual studio 2010.
The easiest way to bind is to set the ItemsSource="{Binding} and in the Window_Loaded event MyDataGrid.DataContext = efData.SomeData
It is working fine, but I don't have data at design time.
Is there an easy way todo?


datagrid binding



I got a problem, since I dont know how to ask my question in a correct way, I added a picture and some code,

as u can below I have a table with several images unchecked.gif / granted.gif now my question is how to bind them to checkboxes / checkboxlist items,
so when I check them the img changes + I can add my code behind them.

It is possible to bind them to a datagrind wich I had in mind at first.

Thnx for your time,!

preview of how it looks atm -> http://yfrog.com/n959532545p



<td align="left" valign="bottom" style="font-weight: bold">Gebruikers</td>
<td align="center" valign="bottom" style="font-weight: bold">Algemeen</td>
<td align="center" valign="bottom" style="font-weight: bold">Financieel</td>
<td align="center" valign="bottom" style="font-weight: bold">Licentie</td>
<td align="center" valign="bottom" style="font-weight: bold">Applicatie</td>
<td align="center"

WPF 4 DataGrid (Binding to DataTable), where store new value of user edit fields? DataGrid architect


I looking for good artical about the DataGrid architecture, logical & visual.

I try to understand whre datagrid store the new value of cell, when user edit row in DataGrid. I realize that DataGrid not hold Copy of data. instead it Hold pointer to DataRow. But DataRow got new value only after user end the line Edit.

the code like:

DirectCast(DataGrid1.CurrentCell.Item, System.Data.DataRowView).Item(1) 

return the DataRow, not the new value during line Edit.

My question

1. Where DataGrid Hold the new value?

2. How access to Visual Element of each cell, like TextBlock inside the cell from Code?

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