Working with datagridview in WPF

When I started working in WPF, the issue found in WPF is that there is no DataGrid. And I couldn’t find any alternative for that. But after few days one of my colleague said there is an option to do it. And it is like setting a View of the Listview control.

So here is the XAML code for a grid like this

<listview Name="MyListView">
</listview><listview .ItemsSource>
<binding Source="{StaticResource AddressData}" Mode="OneWay" />
</listview>
<listview .View>
<gridview AllowsColumnReorder="True">
<gridviewcolumn Header="Name" Width="175" >
</gridviewcolumn><gridviewcolumn .DisplayMemberBinding>
<binding Path="Name" />
</gridviewcolumn>

<gridviewcolumn Header="Address" Width="200">
</gridviewcolumn><gridviewcolumn .DisplayMemberBinding>
<binding Path="Address" />
</gridviewcolumn>

<gridviewcolumn Header="Email" Width="100">
</gridviewcolumn><gridviewcolumn .DisplayMemberBinding>
<binding Path="Email" />
</gridviewcolumn>

</gridview>
</listview>

You can also use the “GridViewColumn.CellTemplate” for modifing the display of the controls. Here I am displaying a checkbox, using a “IsUserExists” boolean property.

<gridviewcolumn Header="?" Width="Auto" >
</gridviewcolumn><gridviewcolumn .CellTemplate>
<datatemplate>
<checkbox IsChecked="{Binding Path=IsUserExists}" />
</datatemplate>
</gridviewcolumn>

You can also create events too, like this

<style x:Key="ListViewItemEvent" TargetType="{x:Type ListViewItem}">
<eventsetter Event="MouseDoubleClick" Handler="lstvMouseDoubleClickEvent" />
</style>

And assign the “ListViewItemEvent” style to the ItemStyle property of the listview. And in the code you will get the ListviewItem object as sender.

Sub lstvMouseDoubleClickEvent(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
Dim oListViewItem As ListViewItem = CType(sender, ListViewItem)
Dim oAddress As Address = CType(oListViewItem.Content, Address)
'Binding code...
End Sub

I got the code of event setting from here

This entry was posted in .Net, .Net 3.0 / 3.5, WPF. Bookmark the permalink.

0 Responses to Working with datagridview in WPF

  1. Odi says:

    >>And I couldn’t find any alternative for that.

    What do you mean? What about Xceed’s amazing DataGrid for WPF, which by the way, is free. It is also about 2 years older and adapted to developer’s needs than any future Microsoft built-in DataGrid for WPF…

  2. anuraj says:

    Odi

    I am not experienced WPF developer. I started working on WPF from the last few days only. And in the project we are using Infragistics XAM DataGrid. It is a requirement from client. I am sharing my experience only. Thanks for giving me the valuable information about WPF datagrid from XCeed.

    Thank you :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>