在Windows应用程序中,我有一个Datagridview
和Textbox
控件。我正在使用存储过程在datagridview中显示一些数据。问题是,当我尝试在datagridview中搜索时,什么也没发生,而且输入到文本框时也很漫长,我发现了
CustomersList.DataSource = dt;
是原因。我对使用存储过程很陌生。我希望有人能够帮助我。
这是用于搜索的代码
Datatable dt;
private void txt_usersearch_TextChanged(object sender, EventArgs e)
{
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString))
{
using (var cmd = new SqlCommand("usp_GetCustomers", con))
{
cmd.CommandType = CommandType.StoredProcedure;
dt.DefaultView.RowFilter = "Full_Name LIKE '%{txt_usersearch.Text}%'";
CustomersList.DataSource = dt;
}
}
}
这段代码是将我的数据显示到datagridview
public 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))
{
dt = new DataTable();
sda.Fill(dt);
dgv.DataSource = dt;
}
con.Close();
}
}
}
}
表格加载
private void ManageCustomer_Load(object sender, EventArgs e)
{
Display.Display_Customer(dt, CustomersList);
}
答案 0 :(得分:0)
为什么要使用“文本更改”事件。那不是一个好方法。我曾经在Dgv EdiitingControl事件上进行实时搜索。您应该在Dgv上进行实时搜索,然后向我展示您编写的存储过程
private void dgv_sales_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
try
{
if (dgv_sales.CurrentCell.ColumnIndex==0)
{
TextBox prodname = e.Control as TextBox;
if (prodname!=null)
{
prodname.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
prodname.AutoCompleteCustomSource = ClientListDropDown();
prodname.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
else if (dgv_sales.CurrentCell.ColumnIndex==7)
{
TextBox prodname = e.Control as TextBox;
if (prodname != null)
{
prodname.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
prodname.AutoCompleteCustomSource = BatchNoDropDown();
prodname.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
else
{
TextBox prodname = e.Control as TextBox;
if (prodname != null)
{
prodname.AutoCompleteMode = AutoCompleteMode.None;
}
}
}
catch (Exception)
{
}
}
答案 1 :(得分:0)
解决了:)
DataView dv = dt.DefaultView;
dv.RowFilter = string.Format("Full_Name like '%{0}%'", txt_usersearch.Text);
CustomersList.DataSource = dv.ToTable();