我知道在SOF中已经多次询问了此问题,但是我向您保证,我尝试了诸如.refresh,.update,在插入后调用该方法之类的所有操作,但是仍然没有任何反应,我只需要再次刷新我的用户控件即可datagridview中的新数据更改。我正在使用存储过程在datagridview上显示数据,也可以进行插入,更新等操作。我希望有人能够帮助我指出我做错了什么或错过了什么。谢谢
这是我的课程,用于在datagridview上显示数据
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,button等之类的控件
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向我的用户控件显示新表单
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);
}
}
}
最后,这是我存储的过程
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
答案 0 :(得分:0)
我玩弄了您发布的代码,并设法使GridView刷新而不必重新加载。
请如下更改显示类别
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();
}
}
}
更新其中适当引用了Display.Display_Customer
的其余代码。
Display.Display_Customer(CustomersList);
或
Display.Display_Customer(_view.CustomersList);
通过上述更改,我设法在更新时刷新DataGridView
。
更新以启用DataGridView搜索:
由于BindingSourse
用作数据源,因此可以利用BindingSourse.Filter
启用搜索。如下更新搜索文本框;
BindingSource dgBS = (BindingSource)CustomersList.DataSource;
dgBS.Filter = string.Format("FileName LIKE '%{0}%'", txtFilter_FileName.Text);