大家好我正在研究C#WPF应用程序,其中我需要通过编辑单元格值在服务器端更新数据(按Enter键)。这是我的代码(如何工作将数据分配给datagrid)。我不知道如何更新datagrid中的值以及将用于哪个事件谢谢(^ _ ^)
后端
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select r.id,r.datee,r.time,c.name,r.description,r.vanda,r.bag,r.price,r.credit,r.debit from records as r, customer as c where r.cusid = c.id and c.name = '" + name + "' and r.datee between '" + startdate.Text + "' and '" + enddate.Text + "' order by r.datee asc", con);
DataSet ds = new DataSet();
adapter.Fill(ds);
data.DataContext = ds.Tables[0];
con.Close();
前端
<DataGrid x:Name="data" ItemsSource="{Binding}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="422" Width="1086" IsReadOnly="True" Margin="0,100,0,0" TabIndex="10"/>
答案 0 :(得分:1)
如果您正在使用数据集,那么我相信您最终会为每个表使用TableAdapter。 TableAdapter类有一个更新方法,该方法将逐行遍历数据集中的表(最初来自数据库的数据的本地副本),并执行每个所需的删除,更新和创建。如果有相关表格,那么通常他们也会得到照顾。 VisualStudio中的DatasetDesigner将为您创建TableAdapter。在MVVM中,您将拥有一个知道DataSet,Tables和TableAdapter的ViewModel,并且可以根据视图中的内容根据需要或期望进行更新。
更具体地说,我使用Linq to SQL,它给了我一个DataContext(类似于数据集,但是对于在数据库上获取完整CRUD的不同方法) 在ViewModel中,首先设置datacontext,然后绑定一个ICommand for on onEowEditEnding来执行自己的处理程序,该处理程序调用DataContext方法SubmitChanges()来更新datagrid中的所有行:
public partial class MyViewModel : ViewModelBase
{
static MyDataContext _dataDC = new MyDataContext();//This class is from the Visual Studio DataDesigner Magic
//There is a constructor here as well . . . skipped for brevity
private ICommand _onRowEditEnding;
public ICommand OnRowEditEnding
{
get { return _onRowEditEnding; }// set as new DelegateCommand(DocumentRowEditEvent) in the VM Constructor
}
public void DocumentRowEditEvent()
{
_dataDc.SubmitChanges(); //Refreshes the view as well
return;
}
}
并且ViewModelBase类是:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChangedEvent(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
这是View(Window XAML):
<Window x:Name="MyWin" x:Class="MYNAMESPACE.Windows.WinMyWin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:MYNAMESPACE.ViewModel"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Title="Files Returned" Height="400" Width="900">
<Grid DataContext="{Binding}">
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch"
ItemsSource="{Binding DataContext.oFRet.View, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
SelectedItem="{Binding DataContext.CurrentReturn, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="rIDColumn" Binding="{Binding UID}" Header="ID" Width="35"/>
<DataGridTextColumn x:Name="xIDColumn" Binding="{Binding xID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="xID" Width="35"/>
<DataGridTextColumn x:Name="rDocumentNumberColumn" Binding="{Binding DocumentNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Document Number" Width="255"/>
<DataGridTextColumn x:Name="rREVColumn" Binding="{Binding REV}" Header="REV" Width="45"/>
<DataGridTextColumn x:Name="rCODEColumn" Binding="{Binding CODE}" Header="CODE" Width="45"/>
<DataGridTextColumn x:Name="rTNTransColumn" Binding="{Binding RTNTrans}" Header="RTN Transmittal" Width="215"/>
<DataGridTextColumn x:Name="rFileNameColumn" Binding="{Binding FileName}" Header="FileName" Width="255"/>
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="RowEditEnding">
<i:InvokeCommandAction Command="{Binding DataContext.OnRowEditEnding, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</Grid>
</Window>
窗口的CodeBehind:
public partial class WinMyWin : Window
{
public WinMyWin(MyViewModel vm)
{
InitializeComponent();
DataContext = vm;
}
}
缺少的是绑定到数据网格的ObservableCollection。它是ViewModel的一部分,并将使用(在我的情况下)Linq to SQL在构造数据时从数据库中获取数据。