2012-05-06 63 views
2

我有一个datagridview与筛选器进行搜索。如果我更新数据库,然后将dgv重置为数据源,则会丢失我的过滤器。我尝试了ResetBindings,但没有帮助。如果我关闭表单并重新打开所做的更改,我只希望它能够“实时”发生。任何建议表示赞赏。刷新绑定datagridview更新后的过滤器c#

我有一个基于SQL视图的数据集。在这个数据集中有一个基于这个视图的表格。 datagridview绑定到这个表。我有几个控件,包括绑定到dgv中的列的文本框和组合框。我有其用于在网格上搜索一个文本框:

private void txtFilterString_TextChanged(object sender, EventArgs e) 
{ 
    ToolStripTextBox tb = (ToolStripTextBox)sender; 

    DataView dv = tILEDataSet.vwTILEAdmin.DefaultView; 

    vwTILEAdminBindingSource.Filter = 
     string.Format(@"PdcProductName LIKE '%{0}%' OR LabelDescription LIKE '%{0}%' OR LabelProductName LIKE '%{0}%'", 
     tb.Text.Trim().Replace("'", "''")); 

    dataGridView1.Refresh();     
} 

通过修饰一个或多个绑定控件我保存改变的,其中更新该表进行更改在DGV一排后:

sql.Append(@"UPDATE [dbo].[LabeledProducts] 
SET [PdcProductName] = @pdcProd 
,[LabelProductName] = @lblProd 
,[LabelDescription] = @lblDesc 
,[Power] = @pwr 
,[Fabrication] = 0 
,[UL_File_Number] = '' 
,[PrePrintedSerial] = 0 
,[ShowOrderOnLabel] = 0 
,[PrivateLabelLogoId] = @plid 
,[AgencyImageId] = @aid 
,[WireDiagConfigId] = @wid 
WHERE PdcProductName = '").Append(pdcProductName).Append("'"); 

using (SqlCommand command = new SqlCommand(sql.ToString(), printConfigTableAdapter.Connection)) 
{ 
    if (vwTILEAdminTableAdapter.Connection.State != ConnectionState.Open) 
     vwTILEAdminTableAdapter.Connection.Open(); 

    LabeledProductsDataTableAdapter.UpdateCommand = command; 
    LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@pdcProd", txtPdcProdName.Text); 
    LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@lblProd", txtLabeledProd.Text); 
    LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@lblDesc", txtLabelDesc.Text); 
    LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@pwr", txtPower.Text); 
    // we need ulfilename and mod 
    LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@plid", LogoId); 
    LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@aid", AgencyId); 
    LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@wid", WireId); 
    DataTable dt = new DataTable(); 

    int rowsAffected = LabeledProductsDataTableAdapter.Update(dt); 
    rowsAffected = command.ExecuteNonQuery(); 

    dataGridView1.Refresh(); 
    //dataGridView1.DataSource = tILEDataSet.vwTILEAdmin; 

    //this.vwTILEAdminBindingSource.ResetBindings(true); 

} 

如果我取消,我设置DataSource行了,我得到了刷新视图,但用于不再生成绑定源过滤器上的文本框的作品,如无论我在文本框中输入什么内容。 Text_Changed事件仍然被调用,但过滤器不再对dgv的内容产生任何影响。

+0

请先发布您的代码,以便我们可以清楚地了解您的问题。什么是过滤器? – jams

+0

你可以发布你的代码吗? – BizApps

+0

vwTILEAdminBindingSource.Filter = \t \t \t \t的String.Format(@ “PdcProductName LIKE '%{0}%' OR LabelDescription LIKE '%{0}%' OR LabelProductName LIKE '%{0}%'”, \t \t \t \t tb.Text.Trim()。Replace(“'”,“''”)); \t \t \t dataGridView1。刷新(); – Gary

回答

2

看起来你的问题很简单。

在这些线路:

​​

您可以设置网格的数据源是vwTILEAdmin,但在你的过滤器的代码,你是这不再是网格的数据源绑定源过滤!

尝试,而不是:

this.vwTILEAdminBindingSource.DataSource = tILEDataSet.vwTILEAdmin; 

this.vwTILEAdminBindingSource.ResetBindings(true); 

而且你可能并不需要对电网的.Refresh()电话 - 方法实际上并不刷新网格的数据源。它只重绘网格客户区域,如果你有一个陈旧的数据源(网格不知道数据已经改变),重绘不会有什么区别。

如果您仍然有问题,可能是网格数据源的更新没有传播 - 这不会引起网格侦听的ListChanged事件以知道何时更新。如果是这种情况,那么你需要清空数据源并重置它。

dataGridView1.DataSource = typeof(List<string>); 
dataGridView1.DataSource = newDataSource; 

在数据源上方的代码被设置为typeof(List),因为这防止任何现有列。然后,您将再次将绑定源设置为网格数据源。虽然我怀疑这是必要的 - 绑定源ResetBindings应该足够了。

+0

直到我关闭表单并重新打开后,仍然没有看到更改,所以我知道数据库正在更新。我试图清空数据源并重置它,以免影响。该行dataGridView1.DataSource = typeof(List);无法编译:错误使用泛型类型'System.Collections.Generic.List '需要1个类型参数。再次感谢您的所有帮助@David。 – Gary

+0

@Gary是的,我只是试图复制你的错误,发现对我来说,过滤仍然有效,当我预计它会被打破。继续挖掘,看看我能否弄清楚你有什么不同。 –

+0

过滤器继续工作,但更新的内容不存在,直到我重新打开表单。 – Gary