where is datagrid _simple_ example?

Apr 23, 2009 at 10:48 AM
is there any really simple example/tutorial/doc for wpf toolkit datagrid?
(unfortunately standard wpf listctrl with gridview is too primitive for me)
i want small editable grid united with own (simple) data structure in memory, say my_struct[5] = {string, string, string, bool};
i have no usage for 50 pages cases like "at first install this 3rd party database, ..."

i started to create own but still failing with something next

with my short code - see down - i can get grid with 4 columns and 2 row
but
- when i double click to cell it crashes
- when i want programaticaly set/get any cell contents i found 
        private DataGridCell TryFindCell(DataGridCellInfo info)
looks too private
thanks (for link-s)
t!

.xaml
<toolkit:DataGrid Name="lv" Grid.Row="0"
AutoGenerateColumns="False"
CanUserAddRows="False"
IsReadOnly="False">
<toolkit:DataGrid.Columns>
<
toolkit:DataGridTextColumn Header="First Name"/>
<
toolkit:DataGridTextColumn Header="Middle Name"/>
<
toolkit:DataGridTextColumn Header="Last Name"/>
</
toolkit:DataGrid.Columns>
</toolkit:DataGrid>

.cs
Microsoft.Windows.Controls.DataGridColumn dc = new Microsoft.Windows.Controls.DataGridCheckBoxColumn();
dc.Header = "Text2";
lv.Columns.Add(dc);
lv.Items.Add("1a");
lv.Items.Add(
"2b");
Apr 23, 2009 at 11:49 AM
Edited Apr 23, 2009 at 11:50 AM
The easiest way is to set up an ObservableCollection<T> where T is your struct type, then data bind each column to a field of your struct type and set DataGrid.ItemsSource to your collection. Something like this, typed up in browser and untested, but you get the idea:

<toolkit:DataGrid Name="dataGrid" Grid.Row="0"
AutoGenerateColumns="False"
CanUserAddRows="False"
IsReadOnly="False">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
<toolkit:DataGridTextColumn Header="Middle Name" Binding="{Binding MiddleName}"/>
<toolkit:DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>

class PersonName
{
public FirstName { get; set; }
public MiddleName { get; set; }
public LastName { get; set; }
}

class PersonNames : ObservableCollection<PersonName>
{ }

class MyWindow : Window // has a DataGrid named dataGrid
{
    private void LoadNamesButtonClickEventHandler(...)
    {
        personNames names = new PersonNames();
        // populate personNames here.
        dataGrid.ItemsSource = personNames;
    }

    private void ShowCurrentSelectedNameButtonClickEventHandler(...)
    {
        PersonName personName = dataGrid.SelectedItem as PersonName;
        if(personName != null)
            MessageBox.Show(string.Format("{0} {1} {2}", personName.FirstName, personName.MiddleName, personName.LastName));
    }
}

You can also manipulate the collection directly in code-behind, but the changes won't show up in the datagrid unless your entity type (PersonName above) implements INotifyPropertyChanged.
Apr 23, 2009 at 2:05 PM
thanks very much, will small changes already works

now problem is values of last column are not displayed into grid - no binding

Microsoft.Windows.Controls.

DataGridColumn dc = new Microsoft.Windows.Controls.DataGridCheckBoxColumn();
dc.Header = "Text2";
//dc.SetBinding(
CheckBox.IsCheckedProperty, "V3"); there is no SetBinding member
lv.Columns.Add(dc);

have i call SetBinding for every dc.GetCellContent(#)?

 

 

CheckBox cb = dc.GetCellContent(1) as CheckBox;

 

cb.SetBinding(

CheckBox.IsCheckedProperty, "V3");

 

 

when to call it? even i call this after 

 

lv.ItemsSource = names;

 

i get cb null

p. s. mentioned small changes:

 

 

public class PersonName

 

 

 

 

 

{

 

static readonly Random s_rand = new Random();

 

 

public string V0 {

 

 

get { return _v0; }

 

 

set { _v0 = value; } }

 

 

public string V1 {

 

 

get { return _v1; }

 

 

set { _v1 = value; } }

 

 

public string V2 {

 

 

get { return _v2; }

 

 

set { _v2 = value; } }

 

 

public bool V3 {

 

 

get { return _v3; }

 

 

set { _v3 = value; }

 

}

 

private string _v0;

 

 

private string _v1;

 

 

private string _v2;

 

 

private bool _v3;

 

 

public PersonName()

 

{

V0 = s_rand.Next(1000, 2000).ToString();

V1 = s_rand.Next(1000, 2000).ToString();

V2 = s_rand.Next(1000, 2000).ToString();

V3 = s_rand.Next(0, 1) != 0 ?

true : false;

 

}

}

 

 

 

Apr 23, 2009 at 2:11 PM
sorry, already see 

DataGridCheckBoxColumn

has Binding member

DataGridColumn

has not