积极答复者
关于wpf中datagrid的绑定问题

问题
-
(新手)我现在从数据库取到一个dataset
DataSet ds = DbHelperOleDb.Query("Select Dname from Tdepartments");
if (ds.Tables[0].Rows.Count > 0)
{
dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
}在xaml中设置了列
<DataGrid AutoGenerateColumns="False" Height="418" HorizontalAlignment="Left" Margin="18,0,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="316" RowEditEnding="dataGrid1_RowEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="部门名称(部门名称指定后尽量不要修改)" HeaderStringFormat="Dname" Binding="{Binding Path=Dname, Mode=TwoWay}" Width="380"/>
</DataGrid.Columns>
</DataGrid>为什么我实现不了 改变datagrid中的值 数据库跟着改变呢?请帮忙详细解答一下,或者给段代码!
答案
-
您好 风中的影子,
改变DataGrid的值只能导致绑定的底层数据改变,例如您代码中的DataView或者DataTable,但是数据并不是会立即刷新到数据库里。要将数据保存到数据库,您得调用一定的更行方法, 您使用的DbHelperOleDb类应该有类似Update或者Save的方法,参数里一般都会包含一个DataTable或者DataSet,调用这类方法就可以了.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.- 已标记为答案 Aland LiModerator 2010年6月2日 10:46
全部回复
-
您好 风中的影子,
改变DataGrid的值只能导致绑定的底层数据改变,例如您代码中的DataView或者DataTable,但是数据并不是会立即刷新到数据库里。要将数据保存到数据库,您得调用一定的更行方法, 您使用的DbHelperOleDb类应该有类似Update或者Save的方法,参数里一般都会包含一个DataTable或者DataSet,调用这类方法就可以了.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.- 已标记为答案 Aland LiModerator 2010年6月2日 10:46