Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I trying to insert data into database from wpf datagrid ,I write this code ,and sometimes it already saved into database so its good but following code open datagrid to be edited when click onto update button & suddendly when click onto datagrid row it opens this column to edit I don't know why any help!!!!!!!!!! by sudden open row to edited without click on update btn it causes error on delete(The object cannot be deleted because it was not found in the ObjectStateManager.)

C#
public partial class VacationTypeScreenInp : UserControl
    {
        public VacationTypeScreenInp()
        {
            InitializeComponent();
        }

        bool isUpdateMode = false;

        private MilitaryAffairsEntities objContext;

        VacationType objVacTypeToEdit = null;        

        private void GetVacationTypes()
        {
            objContext = new MilitaryAffairsEntities();
            var query = from n in objContext.VacationTypes
                        select n;
            dgVacType.ItemsSource = query.ToList();
        }

        private void VacationTypeScreenInp_OnLoaded(object sender, RoutedEventArgs e)
        {
            GetVacationTypes();
            lblGridCount.Content = dgVacType.Items.Count;
        }

        private void DgVacType_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            objVacTypeToEdit = dgVacType.SelectedItem as VacationType;
        }

        private void DgVacType_OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {

            objContext.SaveChanges();
            MessageBox.Show("تم تحديث البيانات بنجاح ...");

        }

        private void DgVacType_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {          
            if (isUpdateMode) //The Row is  edited
            {
                VacationType TempVacType = (from vacType in objContext.VacationTypes
                                            where vacType.VacationTypeID == objVacTypeToEdit.VacationTypeID
                                            select vacType).SingleOrDefault();
                if (TempVacType == null)
                {
                    VacationType employee = new VacationType();

                    FrameworkElement element_1 = dgVacType.Columns[1].GetCellContent(e.Row);
                    if (element_1.GetType() == typeof(TextBox))
                    {
                        var yyVacationTypeName = ((TextBox)element_1).Text;
                        employee.VacationTypeName = Convert.ToString(yyVacationTypeName);
                    }

                    objContext.VacationTypes.Add(employee);                   
                }
                else
                {
                    FrameworkElement element_1 = dgVacType.Columns[1].GetCellContent(e.Row);
                    if (element_1.GetType() == typeof(TextBox))
                    {
                        var yyVacationTypeName = ((TextBox)element_1).Text;
                        objVacTypeToEdit.VacationTypeName = Convert.ToString(yyVacationTypeName);
                    }
                   
                   MessageBox.Show("updated");
                }
                
            }
            
        }

        private void BtnUpdate_OnClick(object sender, RoutedEventArgs e)
        {
            isUpdateMode = true;
            dgVacType.Columns[1].IsReadOnly = false;
            GetVacationTypes();
        }

        private void BtnDelete_OnClick(object sender, RoutedEventArgs e)
        {
            if (objVacTypeToEdit == null)
            {
                MessageBox.Show("لايمكن حذف بيانات فارغة");
            }
            else
            {
                objContext.VacationTypes.Remove(objVacTypeToEdit);
                objContext.SaveChanges();
                GetVacationTypes();
                MessageBox.Show("تم حذف هذا الصف ...");
            }
        }

-------------here is xmal
HTML
<datagrid autogeneratecolumns="False">
          HorizontalAlignment="Left"  AlternatingRowremoved="#FFF3F2E2" Height="Auto"
          Name="dgVacType" VerticalAlignment="Top" MinWidth="450"  Margin="8,8,10,8" 
          RowEditEnding="DgVacType_OnRowEditEnding" CellEditEnding="DgVacType_OnCellEditEnding"
                  SelectionChanged="DgVacType_OnSelectionChanged">
                <datagrid.columns>
                    <datagridtextcolumn binding="{Binding VacationTypeID}" header="VacationTypeID">
                      IsReadOnly="True" x:Name="dgrVacationTypeID" >
                    </datagridtextcolumn>
                    <datagridtextcolumn binding="{Binding VacationTypeName}" header="نوع الأجازة">
                      IsReadOnly="True" x:Name="dgrVacationTypeName" Width="210">
                    </datagridtextcolumn>

                    <datagridtemplatecolumn>
                        <datagridtemplatecolumn.celltemplate>
                            <datatemplate>
                                <Button Content="تحديث البيانات" x:Name="btnUpdate" 
                              Click="BtnUpdate_OnClick" Width="110" BorderBrush="#FF9E0C0C" Background="#FFE3F9FF"></Button>
                            </datatemplate>
                        </datagridtemplatecolumn.celltemplate>
                    </datagridtemplatecolumn>
                    <datagridtemplatecolumn>
                        <datagridtemplatecolumn.celltemplate>
                            <datatemplate>
                                <Button Content="حذف البيانات" x:Name="btnDelete" 
                               Click="BtnDelete_OnClick" Width="110" BorderBrush="#FF9E0C0C" Background="#FFE3F9FF">

                                </Button>
                            </datatemplate>
                        </datagridtemplatecolumn.celltemplate>
                    </datagridtemplatecolumn>
                </datagrid.columns>
            </datagrid>
Posted
Updated 21-Oct-13 5:00am
v2
Comments
Andreas Gieriet 21-Oct-13 11:07am    
Oh, com'on, stop whineing - this is childish ("help!!!!!!!").
Already tryied to debug in the debugger, e.g. by setting breakpoints and single step from there?
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900