我进行了一些搜索,但未能找到任何可行的答案。实际上是三个问题(顺便说一句,我正在使用 WPF)。
1 - 我有一个数据库,我正在使用 TableAdapter 将数据插入其中并显示在数据网格中。插入功能完美,但编辑和删除不起作用。当我更改或删除某些记录时,它确实在数据网格中发生了变化,但是当我重新启动程序时,这两个更改(编辑或删除)都没有保存,而且我不知道为什么,我已经更改了属性复制数据库和其他类似的。这是我的编辑代码:
private void ButtonEditAtiv_Click(object sender, RoutedEventArgs e)
{
string nomeIns = nomeTextBox.Text;
string emailIns = emailTextBox.Text;
string telefoneIns = telefoneTextBox.Text;
string enderecoIns = endereçoTextBox.Text;
string dataNascIns = dataNascimentoDatePicker.SelectedDate.ToString();
InstrutorTableAdapter instrutorTA = new InstrutorTableAdapter();
try
{
instrutorTA.Update(this.academiaSQLDS.Instrutor);
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
2 - 为了将选定的数据网格行数据获取到文本框,我使用了 DataView/DataContext 代码:
private CollectionView dataView;
internal CollectionView DataView
{
get
{
if (dataView == null)
{
dataView = (CollectionView)
CollectionViewSource.GetDefaultView(
this.DataContext);
}
return dataView;
}
}
这个实际上非常适合获取所选数据,但我不知道这是否是正确的方法,因为我发现其他方法都没有这么好用。还有另一种方法可以获取选定的数据网格线并将其放入文本框以编辑数据等吗?
附注:我使用的是 .mdf 本地 SQL 数据库。
最佳答案
尝试使用 ItemSource
而不是 DataContext
然后在 xaml 文本框 Text"{Binding @Col1}"
所以这就是我所做的..
在 xaml DataGrid x:Name="dataGrid1"ItemsSource="{Binding}"
在 xaml Text="{Binding SelectedItem.Grade, ElementName=dataGrid1}"
假设您获得显示数据的数据网格,那么以上就是填充文本框所需的全部内容。
请标记为回答完成。
关于c# - WPF - 通过 TableAdapter、DataContext/Dataview 和 DataGrid 将数据库编辑到 TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33126814/