更新数据时未刷新Datagrid数据,但插入新数据时刷新

时间:2019-03-25 14:50:48

标签: c# entity-framework

我做了一个按钮来刷新datagridview。添加新数据后,该按钮刷新了网格,但是在编辑数据时不刷新。

我的按钮点击代码

private void button2_Click_1(object sender, EventArgs e)
        {
            client_list.Rows.Clear();
            displayClientDetails();
        }

函数displayClientDetails的代码

private void displayClientDetails()
        {
            List<tbl_client> client = db.tbl_client.ToList();
            int i = 0;
            foreach (var data in client)
            {
                client_searchbox.Items.Add(data.client_name);
                client_list.Rows.Add();
                client_list.Rows[i].Cells[0].Value = data.client_id;
                client_list.Rows[i].Cells[1].Value = data.client_name;
                client_list.Rows[i].Cells[2].Value = data.client_address;
                client_list.Rows[i].Cells[3].Value = data.client_contact;
                List<tbl_credit> credit = db.tbl_credit.Where(x => x.client_id == data.client_id).ToList();
                List<tbl_sold> sold = db.tbl_sold.Where(x => x.client_id == data.client_id).ToList();
                long total_purchase = 0;
                foreach (var detail in sold)
                {
                    total_purchase = total_purchase + detail.total_amount;
                }
                client_list.Rows[i].Cells[5].Value = total_purchase;
                foreach (var details in credit)
                {
                    client_list.Rows[i].Cells[4].Value = details.credit_amount;
                    client_list.Rows[i].Cells[6].Value = details.trans_date;
                }
                i++;
            }
        }

1 个答案:

答案 0 :(得分:0)

我不知道我是否正确地理解了您的问题,但是请记住,通常的情况是,当您使用通过引用传递的集合时,单个项的更改不会触发列表刷新,因为引用列表本身与之前的版本相同。您需要清除整个列表,然后再次重新加载所有内容以显示更改