Note that there are some explanatory texts on larger screens.

plurals
  1. POrefreshing bound datagridview with filter after update c#
    primarykey
    data
    text
    <p>I have a datagridview with a filter for doing search. If I update the database and then reset the dgv to the datasource I lose my filter. I tried doing ResetBindings but that didn't help. If I close the form and reopen the changes are there, I'd just like it to happen in "real time". Any advice is appreciated.</p> <p>I have a dataset based on a SQL view. In this dataset there is a table based on this view. The datagridview is bound to this table. I have several controls including textboxes and combo boxes which are bound to columns in the dgv. I have a textbox which is used to search on the grid:</p> <pre><code>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(); } </code></pre> <p>After making changes to a row in the dgv by modifying one or more of the bound controls I save the changes, which update the table:</p> <pre><code>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); } </code></pre> <p>If I uncomment the line where I set the DataSource, I get a refreshed view, but the textbox used to generate the filter on the binding source no longer works, e.g. regardless of what I type in the textbox. The Text_Changed event does still get called, but the filter no longer has any affect on the contents of the dgv. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload