DataGrid not allowing to edit item

£可爱£侵袭症+ 提交于 2019-12-24 13:16:27

问题


I have a problem in Wpf DataGrid. I am adding values in datagrid's first column calledModel Name`. below I am also giving my code to show the problem.

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".

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>

Here Is my Code in 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);
                }


            }


        }

回答1:


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.

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}"/>

assuming you have set the DataContext of your window to itself.

In your constructor you can initialize this collection by newing it and in LoadDeliveryData method add the object to this collection.




回答2:


i got solution here is my code

     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;

        }


来源:https://stackoverflow.com/questions/18952456/datagrid-not-allowing-to-edit-item

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!