将 TextBox 绑定到 DataAdapter

Bind TextBox to DataAdapter

.cs

    DataTable dt;
    NpgsqlDataAdapter adapter;
    NpgsqlCommand selcmd, upcmd, delcmd, inscmd;
    NpgsqlConnection conn;
    NpgsqlTransaction tran;

    public void ReadAndFill()
    {
        conn.Open();
        tran = conn.BeginTransaction();

        selcmd.Connection = conn;
        selcmd.Transaction = tran;

        dt.Clear();
        adapter.Fill(dt);
        dgMain.ItemsSource = dt.DefaultView;
        tran.Commit();
        conn.Close();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    { 
        ReadAndFill();
        DataContext = dt;    
    }

.xaml

<DataGrid x:Name="dgMain" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" AutoGenerateColumns="False"/>
<TextBox x:Name="textBox" Height="23" TextWrapping="Wrap" Text="{Binding name}" Width="120"/>

所以我只能通过textBox编辑DataTable dt的第一行。当您在 DataGrid dgMain 上移动时,textBox 中的数据不会更改。如何解决?

在这种情况下,TextBox 应该绑定到 DataGrid。我还没有找到 TextBox 如何绑定到 DataTable / DataSet / DataAdapter 并保持相同逻辑的示例:TextBox 中的数据将通过在 DataGrid 上移动而更改。