This is a migrated thread and some comments may be shown as answers.

Add row (item) from custom form

2 Answers 43 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Edo Cof
Top achievements
Rank 1
Edo Cof asked on 13 Mar 2010, 01:10 PM
I am trying to build custom form for adding rows.

Part of my xaml looks like:

<Grid HorizontalAlignment="Left" Name="gridStavek" VerticalAlignment="Top" Margin="5,5,5,5"
            <Grid.RowDefinitions> 
                <RowDefinition /> 
                <RowDefinition /> 
            </Grid.RowDefinitions> 
            <TextBox x:Name="editName" Grid.Row="1" LabelText="Ime:" Width="150" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}"></TextBox > 
        </Grid> 

Name is the column in my table.



gridStavek.SetBinding(Grid.DataContextProperty, new Binding { Source = radGrid, Path = new PropertyPath("SelectedItem"), Mode = BindingMode.TwoWay }); 

Now on button click i called radGrid.BeginInsert() and on other button DomainDataSource.SubmitChanges().

If i write some text direct to RadGrid, the text appears in TextBox too. But if i write to TextBox the text doesn't appear in RadGrid, and in my WebServer project i get null for Name property.

I know that i can write something like this:
Groups group = new Groups(); 
group.Name = tbName.Text; 
DS.Groups.Add(group);  //DS = DomainDataSource 
 

But i would rather see that grid do the hard work. The only problem is how to connect TextBlock to Grid.

2 Answers, 1 is accepted

Sort by
0
Accepted
Milan
Telerik team
answered on 18 Mar 2010, 12:15 PM
Hi Matjaz Cof,

The problem is that the TextBox will only update the bound property (Name) when the TextBox loses focus - for example if you press Enter nothing happens because the binding is still not updated. One way to solve the problem is to manually update the binding when Enter key is pressed:

// subscribe for KeyDown of editName
private void editName_KeyDown(object sender, KeyEventArgs e)
{
    TextBox box = (TextBox)sender;
  
    if (e.Key == System.Windows.Input.Key.Enter)
    {
        var bindnig = box.GetBindingExpression(TextBox.TextProperty);
  
        if (bindnig != null)
            bindnig.UpdateSource();
    }
}

Another possible cause of such problems is when the data items do not implement INotifyPropertyChanged.

Hope this helps.

Greetings,
Milan
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Edo Cof
Top achievements
Rank 1
answered on 19 Mar 2010, 07:52 AM
Well, thanks.

I cut some code, so the example would be easier to look. I don't use TextBox, but my own edit and i didn't implement INotifyPropertyChanged.
Tags
GridView
Asked by
Edo Cof
Top achievements
Rank 1
Answers by
Milan
Telerik team
Edo Cof
Top achievements
Rank 1
Share this question
or