如何重新加载WPF网格

时间:2018-09-04 04:55:26

标签: c# wpf grid

This is my code我正在开发WPF应用程序,以在单击“更新”按钮时更新数据库。.我也想在单击“更新”按钮后将从数据库获取的数据重新加载到网格中...请帮我解决这个问题

3 个答案:

答案 0 :(得分:1)

  1. 按钮单击事件代码

    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;
        }
    }
    

答案 1 :(得分:0)

  1. :GetData()用于绑定数据库中的数据
  2. :bindpaggination()用于刷新分页和数据网格的数据。
  3. :在按钮单击事件上插入或更新方法之后,调用bindpaggination()。
  4. :lstvwCustomerslist =您的数据网格名称
  5. :空模型: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;
    }
    

答案 2 :(得分:0)

我刚刚调用了在第一次加载窗口时用来调用动态按钮的方法,再次将其作为Button click事件。因此,通过从数据库中获取更新的列表来再次创建按钮。