1

I want to create a completely dynamic data matrix. For eg,

       Column1 Column2 Column3....
id1      id11    id12     id13...
id2      id21    id22     id23...
.         .        .        .
.         .        .        .

Can anybody help me with the code in XAML that will display the data in the above manner? I mean I am not sure if I should be using listview or datagrid or anything else to display the data.So if anybody could use some example code and help me out with it..

Chris Farmer
  • 24,974
  • 34
  • 121
  • 164
developer
  • 5,178
  • 11
  • 47
  • 72

2 Answers2

4

You could probably use my answer to this Question. It's a subclassed DataGrid used to display, edit and databind 1D or 2D arrays and lists of dynamic size. It can be downloaded from here.

Say you have this 2D array of strings as a Property

public string[][] String2DArray { get; set; }

then you can bind it to the DataGrid2D by adding a reference to the DataGrid2DLibrary.dll and add the namespace

xmlns:dg2d="clr-namespace:DataGrid2DLibrary;assembly=DataGrid2DLibrary"

<dg2d:DataGrid2D Name="c_dataGrid2D" 
                 UseModifiedDataGridStyle="True" 
                 ItemsSource2D="{Binding String2DArray}"/>

And the Output will look like this

alt text

Community
  • 1
  • 1
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
0

Yeah, it sounds like you could make very good use of the <Grid> tag. So, to replicate your example:

<Grid>  
<Grid.ColumnDefinitions>
  <ColumnDefinition Width="100" />
  <ColumnDefinition Width="100" />
  <ColumnDefinition Width="100" />
  <ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
  <RowDefinition Height="25" />
  <RowDefinition Height="25" />
  <RowDefinition Height="25" />
  <RowDefinition Height="25" />
  <RowDefinition Height="25" />
</Grid.RowDefinitions>

<TextBlock Grid.Column="1" Grid.Row="0">Column1</TextBlock>
<TextBlock Grid.Column="2" Grid.Row="0">Column2</TextBlock>
<TextBlock Grid.Column="3" Grid.Row="0">Column3</TextBlock>

<TextBlock Grid.Column="0" Grid.Row="1">id1</TextBlock>
<TextBlock Grid.Column="1" Grid.Row="1">id11</TextBlock>
<TextBlock Grid.Column="2" Grid.Row="1">id12</TextBlock>
<TextBlock Grid.Column="3" Grid.Row="1">id13</TextBlock>

<TextBlock Grid.Column="0" Grid.Row="2">id2</TextBlock>
<TextBlock Grid.Column="1" Grid.Row="2">id21</TextBlock>
<TextBlock Grid.Column="2" Grid.Row="2">id22</TextBlock>
<TextBlock Grid.Column="3" Grid.Row="2">id23</TextBlock>

<TextBlock Grid.Column="0" Grid.Row="3">.</TextBlock>
<TextBlock Grid.Column="1" Grid.Row="3">.</TextBlock>
<TextBlock Grid.Column="2" Grid.Row="3">.</TextBlock>
<TextBlock Grid.Column="3" Grid.Row="3">.</TextBlock>

<TextBlock Grid.Column="0" Grid.Row="4">.</TextBlock>
<TextBlock Grid.Column="1" Grid.Row="4">.</TextBlock>
<TextBlock Grid.Column="2" Grid.Row="4">.</TextBlock>
<TextBlock Grid.Column="3" Grid.Row="4">.</TextBlock>

</Grid>

You can also use the WPF datagrid, available in WPF 4.0. If you cannot use the 4.0 Framework, than you still could use the datagrid under the codeplex release for .NET 3.5 SP1. See WPF Toolkit

You could also use the ListView, yes. WPF is very flexible, so you have a lot of choices. Program Grid tags as above, or use a datagrid or listBox or listView with an ItemSource set on the 3 latter choices.

Lane
  • 2,669
  • 3
  • 25
  • 38
  • 1
    He said he wanted to create completely dynamic data matrix. What would you use if there is matrix of 200x200 size? – Novitzky Nov 09 '10 at 21:32
  • Well, the XAML can be created on the fly. Do a simple Google search on Dynamic XAML. Check out this link: http://msdn.microsoft.com/en-us/library/cc189044%28VS.95%29.aspx. But I'd try a DataGrid in WPF 4.0 first. Barring both those options, I'd buy Exceed's datagrid. It is said to perform very well. – Lane Nov 09 '10 at 21:39
  • That is correct..My xaml will be created dynamically..is it possibleusing listview..can you please show me how.. – developer Nov 09 '10 at 21:42
  • XAML should be very simple. e.g and it shouldn't be created on the fly. It should render the nice view on the fly but this is different story. – Novitzky Nov 09 '10 at 21:44