问题
I have a DataGridView and an Edit button. When I click the Edit button and change a field, I press Save and the DataGridView comes up again. The problem is that my changes aren't showing.
Using the debugger, I have looked in the DGV and in the BindingSource and the correct data is there. It is just not displaying in the DGV.
Here is my code - I do realize that it is semi-redundant but, at this point, I'm four hours into it and am willing to try anything.
this.iSOXTableAdapter.FillByISOX(this.MSDataSet.ISOX);
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = iSOXBindingSource;
iSOXDataGridView.DataSource = null;
iSOXDataGridView.DataSource = bindingSource;
bindingSource.ResetBindings(false);
this.iSOXDataGridView.Refresh();
I have looked at the following questions (and many others) and tried their suggestions to no avail:
Datagridview not updating correctly
dataGridView not updating c#?
DataGridView not updating in c#
Best way to refresh DataGridView when you update the base data source
How to refresh or show immediately in datagridview after inserting?
I appreciate any help or suggestions or ideas for workarounds. Thank you so much for looking at this.
***************** EDIT *******************
Here is the code for the save button - I know it is working because after I requery the data, it is in the binding source and in the DGV. This code is in a separate add/edit form:
private void BtnSave_Click(object sender, EventArgs e)
{
if (ValidateForm())
{
ISOXBindingNavigatorSaveItem_Click(sender, e);
this.Close();
}
else
{
MessageBox.Show("Not Validated - Could not Save");
}
}
Here is full code for the user control with the DGV on it:
public partial class FindISOXControl : UserControl
{
private bool gridInitialized = false;
public delegate void ItemHasBeenSelected(object sender, SelectedItemEventArgs e);
public event ItemHasBeenSelected SelectedItem;
public class SelectedItemEventArgs : EventArgs
{
public int SelectedChoice { get; set; }
}
public bool First = true;
public FindISOXControl()
{
InitializeComponent();
FillTableAdapter();
iSOXDataGridView.Columns.Cast<DataGridViewColumn>().ToList().ForEach(f => f.SortMode = DataGridViewColumnSortMode.NotSortable);
}
public void FillTableAdapter()
{
this.iSOXTableAdapter.FillByISOX(this.MSDataSet.ISO);
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = iSOXBindingSource;
iSOXDataGridView.DataSource = null;
iSOXDataGridView.DataSource = bindingSource;
bindingSource.ResetBindings(false);
this.iSOXDataGridView.Refresh();
setGridData();
}
public void UpdateISOXText(string pISOX = "")
{
this.txtFind.Text = pISOX;
txtFind.Refresh();
}
DataTable dt = new DataTable();
public void btnFind_Click(object sender, EventArgs e)
{
{
setGridData();
}
}
public void setGridData()
{
GetData();
if (iSOXDataGridView.RowCount > 0)
{
EventArgs e = new EventArgs();
iSOXDataGridView_SelectionChanged(null, e);
}
}
public void txtISOX_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Tab)
{
setGridData();
}
}
//Query database
public void GetData()
{
String searchValue = txtFind.Text.Trim().ToUpper();
int rowIndex = -1;
foreach (DataGridViewRow row in iSOXDataGridView.Rows)
{
if (row.Cells[0].Value.ToString().Contains(searchValue))
{
rowIndex = row.Index;
break;
}
}
if (rowIndex == -1)
{
foreach (DataGridViewRow row in iSOXDataGridView.Rows)
{
if (row.Cells[4].Value.ToString().ToUpper().Contains(searchValue))
{
rowIndex = row.Index;
break;
}
}
}
if (rowIndex == -1)
{
if (searchValue != null && searchValue !="")
{
MessageBox.Show(searchValue + " Not Found");
}
}
else
{
iSOXDataGridView.Rows[rowIndex].Selected = true;
iSOXDataGridView.CurrentCell = iSOXDataGridView.Rows[rowIndex].Cells[0];
}
}
public void iSOXDataGridView_SelectionChanged(object sender, EventArgs e)
{
if (iSOXDataGridView.CurrentRow != null)
{
int Row = iSOXDataGridView.CurrentRow.Index;
if (gridInitialized)
{
txtFind.Text = iSOXDataGridView[0, Row].Value.ToString();
// 6 is the ID column
DataGridViewCellEventArgs ev = new DataGridViewCellEventArgs(6, Row);
iSOXDataGridView_CellDoubleClick(sender, ev);
}
}
}
private void iSOXDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.RowIndex < iSOXDataGridView.RowCount)
{
// 6 == id column
int choice = (int)iSOXDataGridView[6, First ? 0 : e.RowIndex].Value;
this.SelectedItem(this, new SelectedItemEventArgs { SelectedChoice = choice });
}
}
private void iSOXDataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
{
// 6 is the ID column
DataGridViewCellEventArgs ev = new DataGridViewCellEventArgs(6, e.RowIndex);
if (e.RowIndex != 0)
{
First = false;
iSOXDataGridView_CellDoubleClick(sender, ev);
}
}
private void iSOXDataGridView_RowLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == 0 && e.ColumnIndex == 0)
{ First = true; }
}
}
Here is the code from the main form with the user control on it after it returns from the edit:
uc.FillTableAdapter();
********* EDIT --> Code that is opening Edit form:
AddEditISOX aeix = new AddEditISOX("E", currentISOX);
aeix.ShowDialog();
ISOX_Load(sender, e);
ISOX_Load() calls uc.FillTableAdapter();
回答1:
A DataGridView sets up bindings the first time you assign the DataSource. The problem is that subsequent DataSource assignments, if the assignments have a different structure from the initial assignment, will fail because the bindings are now "off"
You need to reset the DataGridView thusly so that the data is bound a new. (The link is for VB but you just need to know the methods to call. Even copy/paste would be overkill.)
回答2:
Maybe you don't have proper update sql commands in the adapter add these commands to your intialize method
iSOXTableAdapter.Adapter.MissingSchemaAction = MissingSchemaAction.Error;
iSOXTableAdapter.Adapter.MissingMappingAction = MissingMappingAction.Error;
maybe the generated error will tell you what are you missing there
回答3:
bind your data to the View of table and it would automatically get updates.
回答4:
I finally figured this blasted thing out. In my Edit Button Click method, I need to call the user control fill table adapter method again.
private void BtnEdit_Click(object sender, EventArgs e)
{
if (currentISOX != 0)
{
AddEditISO aei = new AddEditISOX("E", currentISOX);
aei.ShowDialog();
findISOXControl1.FillTableAdapter();
}
else
{
MessageBox.Show("Please Make a Selection First");
}
}
Thanks for everyone's thoughts, advice and input. It was very helpful and eventually led me to the solution.
来源:https://stackoverflow.com/questions/48732033/datagridview-not-updating-in-c-sharp-when-binding-source-is-correct