1

我在 WpfDataGrid. I am adding values in datagrid's first column called模型名称中有问题。下面我也给出了我的代码来显示问题。

所以我的问题是第一列将显示数据库中的值,而其他列可以写。所有值都将保存在某个数据库表中。当我使用datagrid.Items.Add(data);then 它给出了第一列中的值,工作正常但是如果我开始在其他列中写入它会给出错误

“不允许编辑项目”。

这是我在 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);
                }


            }


        }
4

2 回答 2

2

should not update the Items直接将您的DataGrid但设置为ItemsSource实现IEditableCollectionView接口的集合以允许编辑。该界面具有EditItems()让编辑发生的功能。

所以为了解决这个问题。在您的窗口中创建ObservableCollection<BikesOrderObjects>属性说 MyBikesOrders 并将其绑定DataGrid ItemsSource到它

<DataGrid HorizontalAlignment="Center" ItemsSource="{Binding MyBikesOrders}"/>

假设您已将DataContext窗口设置为自身。

在您的构造函数中,您可以通过新建它来初始化此集合,并在LoadDeliveryData方法中将对象添加到此集合中。

于 2013-09-23T06:10:41.027 回答
0

我得到了解决方案这是我的代码

     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
                            });
            List<BikesOrderObjects> data = new List<BikesOrderObjects>();
            foreach (var item in BikesOrderData)
            {

                for (int i = 0; i < item.qty; i++)
                {
                    BikesOrderObjects datae = new BikesOrderObjects();
                    datae.ModelName = item.ModelName;
                    data.Add(datae);
                }

            }

            DeliveryGrid.ItemsSource = data;

        }
于 2013-09-23T06:50:07.927 回答