-3

This is my code I am developing a WPF application to update the DB when Update button is clicked.. also I want to reload the data which I am getting from the DB to a grid after update button clicked...Please help me to solve this problem

3 Answers3

1
  1. Button Click Event Code

    private void btnsave_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (!IsPageValid())
            {
    
            }
            else
            {
                feesGroup_Model.Name = txtname.Text;
                feesGroup_Model.Description = txtdiscription.Text;
    
                feesGroups_ViewModel.FeesGroup_Insert(feesGroup_Model);
                lstvwCustomerslist.ItemsSource = feesGroups_ViewModel.BindFeesGroupData(txtSearch1.Text);
    
                txtname.Text = string.Empty;
                txtdiscription.Text = string.Empty;
                lblmsg.Visibility = Visibility.Hidden;
                errorgrid.Visibility = Visibility.Collapsed;
    
                bindpaggination();
            }
        }
        catch (Exception ex)
        {
            var abc = ex.ToString();
            throw;
        }
    }
    
Aftab Lala
  • 136
  • 6
0
  1. : GetData() for bind data from database
  2. : bindpaggination() For refresh paggination and data for your datagrid.
  3. : bindpaggination() call after insert or update method on your button click event.
  4. : lstvwCustomerslist = YOUR DATAGRID NAME
  5. : Null Model : ObservableCollection list = new ObservableCollection();

    private void bindpaggination()
    {
        var myList = GetData();
        lstvwCustomerslist.ItemsSource = myList.Take(numberOfRecPerPage);
        int count = myList.Take(numberOfRecPerPage).Count();
        lblpageInformation.Content = count + " of " + myList.Count;
    }
    private ObservableCollection<FeeType_Model> GetData()
    {
        ObservableCollection<FeeType_Model> list = new ObservableCollection<FeeType_Model>();
        var dt = feesType_ViewModel.BindALLFeesTypeData();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            FeeType_Model feeType_Model = new FeeType_Model();
            feeType_Model.Id = Convert.ToInt32(dt.Rows[i]["id"]);
            feeType_Model.Type = dt.Rows[i]["type"].ToString();
            feeType_Model.Code = dt.Rows[i]["code"].ToString();
            feeType_Model.Is_active = dt.Rows[i]["is_active"].ToString();
            feeType_Model.Created_at = dt.Rows[i]["created_at"].ToString();
            feeType_Model.Description = dt.Rows[i]["description"].ToString();
    
            list.Add(feeType_Model);
        }
        return list;
    }
    
Aftab Lala
  • 136
  • 6
  • If You not use MVVM in your WPF Code. after insertion or updation of data initially you have to null datagrid and than bind DataGrid again After That refresh datagrid using datagridname.items.refresh(), it shows updated data in datagrid. – Aftab Lala Sep 04 '18 at 05:14
  • I want to know the way of reloading a Grid.. not a Data Grid – Chameena Hiruni Sep 04 '18 at 05:15
  • Yes above code just example how to reload data if you want to work with DB in datagrid. you can also refresh your wpf grid just pass X:NAME for grid and use its name in Constructor when press Insert or update button click event, call constructor and refresh GRID (also close all tabs one open that one grid). I Hope its work for you. – Aftab Lala Sep 04 '18 at 05:33
  • Please can you send me your snippet or code? so effectively i will help you to solve your problem – Aftab Lala Sep 04 '18 at 05:48
  • I have insert that one in the drive and link here.. Sorry I don't know another way to share the code – Chameena Hiruni Sep 04 '18 at 05:58
  • Above Two Method I Sent And Than button click event just insert like this its 100% work for me i develop whole project in wpf so try to understand this code it will work you. – Aftab Lala Sep 04 '18 at 06:15
  • I'll try...Thankyou very much – Chameena Hiruni Sep 04 '18 at 06:22
  • If solved your problem please up-vote for my answer!! Thanks – Aftab Lala Sep 04 '18 at 06:25
0

I just called the method which I was using to call the dynamic buttons in the first loading of the window again to the Button click event. So the buttons were created again by taking the updated list from the Database.