Show immediately data in datagridview After the Insertion/Updating
我知道在 SOF 中已经多次询问过这个问题,但我向您保证,我尝试了所有方法,例如 .refresh、.update、插入后调用该方法等。但仍然没有任何反应,我需要再次刷新我的用户控件以查看datagridview 中的新数据更改。我正在使用存储过程在我的 datagridview 上显示数据,也用于插入、更新等。我希望有人能够帮助我指出我做错了什么或我错过了什么。谢谢
这是我在 datagridview 上显示数据的类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public static class Display { public static void Display_Customer(DataTable dt, DataGridView dgv) { using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString)) { using (var cmd = new SqlCommand("usp_GetCustomers", con)) { con.Open(); cmd.CommandType = CommandType.StoredProcedure; using (var sda = new SqlDataAdapter(cmd)) { sda.Fill(dt); var bsource = new BindingSource(); bsource.DataSource = dt; dgv.DataSource = bsource; sda.Update(dt); } con.Close(); } } } } |
这是我的用户控件,我的控件如 datagridview、按钮等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public partial class ManageCustomer : UserControl { public ManageCustomer() { InitializeComponent(); } public DataTable dt = new DataTable(); private void ManageCustomer_Load(object sender, EventArgs e) { Display.Display_Customer(dt, CustomersList); Customization._DGVWidth(CustomersList); } private void CustomersList_SelectionChanged(object sender, EventArgs e) { Register.CustomerSelection(CustomersList, lbl_id, lbl_name, lbl_gender, lbl_contact,lbl_email, lbl_address, PreviewImage); } private void Btn_Update_Click(object sender, EventArgs e) { var uc = new UpdateCustomer(this).ShowDialog(); } private void CustomersList_RowEnter(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { DataGridViewRow row = CustomersList.Rows[e.RowIndex]; Variables.ID = row.Cells["Customer ID"].Value.ToString(); } } } |
这是一个基于ID从datagridview获取数据的表单
PS:Btn_Update 向我的 usercontrol
显示新表单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public partial class UpdateCustomer : Form { ManageCustomer _view; public UpdateCustomer(ManageCustomer view) { InitializeComponent(); UpdateC.CustomerInformation(Variables.ID, lbl_path, PreviewImage, txt_name, txt_contact, txt_email, txt_address); this._view = view; } private void btn_update_Click(object sender, EventArgs e) { using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString)) { UpdateC._Update(lbl_path.Text, txt_name.Text, txt_contact.Text, txt_email.Text, txt_address.Text); Display.Display_Customer(_view.dt, _view.CustomersList); } } } |
最后,这是我的存储过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | USE [SalesInventory] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[usp_GetCustomers] AS BEGIN SET NOCOUNT ON; SELECT CustomerID as 'Customer ID', Images, Full_Name, Gender, Contact_Number as 'Contact Number', Email, Home_Address as 'Address' FROM Customer_List END |
我使用了您发布的代码并设法让 GridView 刷新而无需重新加载。
请更改显示类如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public static void Display_Customer3(DataGridView dgv) { using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString)) { using (var cmd = new SqlCommand("usp_GetCustomers", con)) { con.Open(); cmd.CommandType = CommandType.StoredProcedure; using (var sda = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); // Initiate the datatable here to avoid getting duplicate data during 'sda.Fill(dt)' sda.Fill(dt); var bsource = new BindingSource(); bsource.DataSource = dt; dgv.DataSource = bsource; //sda.Update(dt); } con.Close(); } } } |
更新代码的其余部分,其中
1 | Display.Display_Customer(CustomersList); |
或
1 | Display.Display_Customer(_view.CustomersList); |
随着上面的改变,我设法在更新时刷新了
更新以启用 DataGridView 搜索:
由于
1 2 | BindingSource dgBS = (BindingSource)CustomersList.DataSource; dgBS.Filter = string.Format("FileName LIKE '%{0}%'", txtFilter_FileName.Text); |