将新项目添加到数据库后,如何自动更新列表视图 [英] How to update listview automatically after adding a new item into DB

查看:78
本文介绍了将新项目添加到数据库后,如何自动更新列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#WPF MVVM.因此,在XAML中,存在一个绑定到对象的列表视图,用于根据选项卡显示来自sql数据库的不同信息.

I'm using C# WPF MVVM. So in XAML there is a listview, which is binded to an object, used to show different information from sql database depending on tab.

例如.我有两种形式:一种是显示信息,另一种是用于输入信息.在以另一种形式输入新信息之后,如何以一种形式自动更新列表视图?因为现在我必须切换选项卡才能更新列表视图.

For example. I have two forms: one is that shows information and another that is used to input information. How can I automatically update the listview in one form, after new information was entered in another form? Because now I have to switch tabs to get the listview updated.

推荐答案

在将新信息输入表单后,请尝试调用您自己的方法,该方法会将您的信息更新为列表视图. 因此,您可以使用一些事件,例如.当您单击将新数据添加到表单中的按钮时,可以调用DataContentChanged或您的更新方法. 刷新方法的示例应如下所示:

After you enter new information into a form, try to invoke your own method, which will update your information into a list view. So you can use some event eg. DataContentChanged or your update method can be called when u click the button which adds new data into your form. Example of refresh method should look like this:

public void lbRefresh()        
{
    //create itemsList for listbox
    ArrayList itemsList = new ArrayList();
    //count how many information you wana to add
    //here I count how many columns I have in dataGrid1
    int count = dataGrid1.Columns.Count;
    //for cycle to add my strings of columns headers into an itemsList
    for (int i = 0; i < count; i++)
    {
        itemsList.Add(dataGrid1.Columns[i].Header.ToString());
    }
    //simply refresh my itemsList into my listBox1
    listBox1.ItemsSource = itemsList;
}

要完成并解决您的问题,请尝试使用以下代码段:

To finish and solve your problem, try to use this snippet of code:

//some btn_Click Event in one window 
//(lets say, its your callback " to update" button in datagrid)
private void Button_Click_1(object sender, RoutedEventArgs e)
{
    //here you doing somethin
    //after your datagrid got updated, try to store the object, 
    //which u want to send into your eg. listbox

    data[0] = data; //my stored data in array

    //for better understanding, this method "Button_Click_1" is called from Window1.xaml.cs
    //and I want to pass information into my another window Graph1.xaml.cs

    //create "newWindow" object onto your another window and send "data" by constuctor
    var newWindow = new Graph1(data); //line *
    //you can call this if u want to show that window after changes applied
    newWindow.Show();
}

之后,您的Graph1.xaml.cs应该如下所示:

After that your Graph1.xaml.cs should look like this:

public partial class Graph1 : Window
{//this method takes over your data u sent by line * into previous method explained
    public Graph1(int[]data) 
    {
        InitializeComponent();
        //now you can direcly use your "data" or can call another method and pass your data into it
        ownListBoxUpdateMethod(data);

    }
    private void ownListBoxUpdateMethod(int[] data)
    {
        //update your listbox here and its done ;-)
    }

这篇关于将新项目添加到数据库后,如何自动更新列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆