Home Forums WPF controls Xceed DataGrid for WPF Question about databinding

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #26406 |

    Hello,

    I am still a bit new about Databinding. So forgive this novice question.

    I am hiding one of my columns in my Datagrid. The column is a text field. I want to display this text field in a textbox below the datagrid. Should I be trying to databind this textbox to the datagrid’s column that I am hiding? Or to the DataGridCollectionView that the Datagrid uses? Or to the actual Datatable?

    (I also want to make it so that if the textbox loses focus that it will save the changes back to the database)

    Imported from legacy forums. Posted by David (had 5069 views)

    Xceed Support
    Member
    Post count: 5658

    It all depends on what you want to do. If you just want to display information about, for example, the current item, then you could bind the text box to the corresponding cell in the current item. If you want to be abale to edit the value in sync with the other cells in the same row, then you should bind it to the column. If you want to edit it independantly of the other cells in the same row, then binding it directly to the data table would be your best option.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 385 views)

    User (Old forums)
    Member
    Post count: 23064

    It sounds like I want the 2nd option you said…
    (Basically if someone selects the 5th row in the datagrid I want the Textbox to get the text from the Notes column from the 5th row.)

    I have been trying to bind it to the Columns property….but it never displays anything in the textbox.

    My code is:

    DataGridCollectionView dataGridCollectionView = new DataGridCollectionView(table_Site_ID.DefaultView);

    dataGridControl_Site_ID.ItemsSource = dataGridCollectionView;

    dataGridControl_Site_ID.Columns[ “Notes” ].Visible = false;

    TextBox_Site_Notes.DataContext = dataGridControl_Site_ID.Columns[“Notes”];

    XAML code is:

    <TextBox Margin=”2.929,28,228.337,21.384″ TextWrapping=”Wrap” x:Name=”TextBox_Site_Notes” Text=”{Binding Notes, NotifyOnSourceUpdated=True}” />

    Any idea on what I am doing wrong?

    Imported from legacy forums. Posted by David (had 650 views)

    Xceed Support
    Member
    Post count: 5658

    Set the DataContext of the TextBox to the grid’s GlobalCurrentItem (or CurrentItem if you are not use master/detail) and set the Path to the name of the column.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 385 views)

    User (Old forums)
    Member
    Post count: 23064

    Hmm I have tried doing that…and also tried many variations of it but it still doesn’t seem to work. The textbox is always empty.

    table_Site_ID.ChildRelations.Add(new DataRelation(“Site_Contacts”, table_Site_ID.Columns[“ID”], table_Site_Contacts.Columns[“Site_ID”]));

    table_Site_ID.ChildRelations.Add(new DataRelation(“Scheduler”, table_Site_ID.Columns[“ID”], table_Scheduler.Columns[“ID”]));

    DataGridCollectionView dataGridCollectionView = new DataGridCollectionView(table_Site_ID.DefaultView);

    dataGridControl_Site_ID.ItemsSource = dataGridCollectionView;

    dataGridControl_Site_ID.Columns[ “Notes” ].Visible = false;

    TextBox_Site_Notes.DataContext = dataGridControl_Site_ID.GlobalCurrentItem;

    XAML code is:

    <TextBox Margin=”2.929,28,228.337,21.384″ TextWrapping=”Wrap” x:Name=”TextBox_Site_Notes” Text=”{Binding Path=Notes, NotifyOnSourceUpdated=True}” />

    Imported from legacy forums. Posted by David (had 594 views)

    User (Old forums)
    Member
    Post count: 23064

    Anyone have any ideas why this isn’t working?

    Imported from legacy forums. Posted by David (had 671 views)

    Xceed Support
    Member
    Post count: 5658

    If I assume that you are using a DataTable whose DataItems contain a field named Notes, then you should try [Notes] to access the indexed field (again, assuming that you are dealing with System.Data.DataRow as the underlying data item type).

    What you could also do is a binding like so and forget about changing the DataContext:

    <TextBox Text=”{Binding ElementName=nameOfTheGrid, Path=GlobalCurrentItem[Notes]”}/>

    Imported from legacy forums. Posted by Jenny [Xceed] (had 346 views)

    User (Old forums)
    Member
    Post count: 23064

    Ahh perfect! Thanks again Jenny for the aid! You rock!

    Imported from legacy forums. Posted by David (had 6246 views)

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.