DataGrid不允许编辑项目 [英] DataGrid not allowing to edit item
问题描述
我在Wpf DataGrid中遇到问题。我正在datagrid的第一列
模型名称中添加值。下面,我还提供了代码以显示问题。
I have a problem in Wpf DataGrid. I am adding values in datagrid's first column called
Model Name`. below I am also giving my code to show the problem.
所以我的问题是第一列将显示数据库中的值,其他列可写。所有值将保存在某些数据库表中。当我使用 datagrid.Items.Add(data);
时,它给出第一列中的值,可以正常工作,但是如果我开始写其他列,则给出错误
So my problem is first column will show the values from database and the other columns can be writeable. All the values will save in some database table. when I use datagrid.Items.Add(data);
then it gives the values in first column, works fine but if I start writing in other columns it gives error
不允许编辑。
"Edititem is not allowed".
此处是我在WPF中的代码
Here is my code in WPF
<DataGrid HorizontalAlignment="Center"
Name="DeliveryGrid" IsReadOnly="False"
AutoGenerateColumns="False"
LoadingRow="DataGrid_LoadingRow"
Margin="0,85,0,0"
VerticalAlignment="Top"
Height="235"
Width="462">
<DataGrid.Columns>
<DataGridTextColumn Header="Model" Width="90" Binding="{Binding ModelName}" x:Name="ModelName" />
<DataGridTextColumn Header="Engine No" Width="90" x:Name="EngineNo" />
<DataGridTextColumn Header="Chasis No" Width="90" x:Name="ChasisNo" />
<DataGridTextColumn Header="Authority No" Width="90" x:Name="AuthorityNo" />
<DataGridTextColumn Header="Price" x:Name="Price" />
</DataGrid.Columns>
</DataGrid>
这是我在C#中的代码
public class BikesOrderObjects
{
public string ModelName { get; set; }
public int ModelID { get; set; }
public string BEngineNo { get; set; }
public string BChasisNo { get; set; }
public string BAuthorityNo { get; set; }
public double Price { get; set; }
public int qty { get; set; }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
RST_DBDataContext conn = new RST_DBDataContext();
LoadDeliveryData();
}
private void LoadDeliveryData()
{
RST_DBDataContext conn = new RST_DBDataContext();
var BikesOrderData = conn.TblBikesOrdersDetails
.Where(TblBikesOrdersDetail => TblBikesOrdersDetail.BOId == 1)
.Select(TblBikesOrdersDetail => new
{
qty = TblBikesOrdersDetail.BQty,
ModelID = TblBikesOrdersDetail.ModelId,
ModelName = TblBikesOrdersDetail.TblBikeModel.ModelName
});
foreach (var item in BikesOrderData)
{
for (int i = 0; i < item.qty; i++)
{
var data = new BikesOrderObjects { ModelName = item.ModelName };
DeliveryGrid.Items.Add(data);
}
}
}
推荐答案
您不应直接更新
DataGrid
,而是将 ItemsSource
设置为实现 IEditableCollectionView
接口的集合,以便进行编辑。该接口具有 EditItems()
函数,可以进行编辑。
You should not update the Items
directly of your DataGrid
but rather set the ItemsSource
to the collection that implements IEditableCollectionView
interface in order to allow the editing. This interface has function EditItems()
which let the editing happen.
因此,为了解决此问题。在窗口中创建 ObservableCollection< BikesOrderObjects>
属性,例如说MyBikesOrders,然后将 DataGrid ItemsSource
绑定到它上
So in order solve this problem. Create the ObservableCollection<BikesOrderObjects>
property say MyBikesOrders in your window and bind the DataGrid ItemsSource
to it like
<DataGrid HorizontalAlignment="Center" ItemsSource="{Binding MyBikesOrders}"/>
假设您已经设置了 DataContext
在您的构造函数中,可以通过对其进行初始化来初始化此集合,并在 LoadDeliveryData
方法中添加对象此收藏集。
In your constructor you can initialize this collection by newing it and in LoadDeliveryData
method add the object to this collection.
这篇关于DataGrid不允许编辑项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!