开发者

Not clear previous data in list<T>

开发者 https://www.devze.com 2023-03-12 07:11 出处:网络
I insert data to list and bind it to data-grid. And when i try to reinsert data , the previous data is cleared .

I insert data to list and bind it to data-grid.

And when i try to reinsert data , the previous data is cleared .

I want the previous data not clear and new data add in list

 int count = 0;
    private void button_insert_Click(object sender, RoutedEventArgs e)
    {
        Table_infodetail_print tip = new Table_infodetail_print();
        List<Table_infodetail_print> ltip = new List<Table_infodetail_print>();
        tip.nation_code = nation_code;
        tip.services_discription = comboBox_services.SelectedValue.ToString();
        tip.additional_price = additional_price;
        tip开发者_如何转开发.lot_patient = lot_patient;
        tip.price = price;
        tip.tariff = tariff;
        ltip.Insert(count, tip);
        var select_details = from t in ltip
                             select t;
        dataGrid_insert_services.ItemsSource = ltip;
        count++;
    }

Please advise me


Well you are creating a new instance of ltip every time you press the button. Try declaring ltip globaly like your count variable.

int count = 0;
List<Table_infodetail_print> ltip = new List<Table_infodetail_print>();

private void button_insert_Click(object sender, RoutedEventArgs e)
{
    Table_infodetail_print tip = new Table_infodetail_print();        
    tip.nation_code = nation_code;
    tip.services_discription = comboBox_services.SelectedValue.ToString();
    tip.additional_price = additional_price;
    tip.lot_patient = lot_patient;
    tip.price = price;
    tip.tariff = tariff;
    ltip.Insert(count, tip);
    var select_details = from t in ltip
                         select t;
    dataGrid_insert_services.ItemsSource = ltip;
    count++;
}

And the count variable isn't needed because it's include in the list. ltip.Count

0

精彩评论

暂无评论...
验证码 换一张
取 消