简体   繁体   中英

C#/XAML: Get Textbox value from DataTemplate Textbox in DataGridView

The sitatuion:

I've a DataGridView on my WPF Application that is connected to my SQL server. I created a Textbox, which doesn't really exist, inside a DataTemplate.

<DataGridTemplateColumn Header="Amount">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBox x:Name="txtAmount"/>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

This ensures that every column has a Textbox. Now I want to get the Textbox value from the currentcell or the last cell where some value is put in (on the DataGridView) and I want use this value to insert the value in another column of the database table. I have approached many sources but unfortunately I don't know how to implement this inside my program. I tried something with Binding, but that didn't work out.

I hope someone can help me with my problem.

If you set the data context for your DataGrid, datagrid's row inherits that ( row's data context is a single element from the collection that you are binding your DataGrid to).

If you want to bind to some value outside DataGrid, that's a tricky case, because the column doesn't belong to the visual or logical tree of the DataGrid. You have to use some kind of proxy

Make use of Textbox.TextChanged event. If you are using MVVM, bind a method with converter to obtain data from the cell that has changed.. Use this value to apply on any other column.

You can make use of converterparameter to bind in the cell where you will need to make changes...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM