绑定源正确时,DataGridView无法在C#中更新 [英] DataGridView Not Updating in C# When Binding Source is Correct
问题描述
我有一个DataGridView和一个Edit按钮.当我单击编辑"按钮并更改字段时,我按保存",然后再次出现DataGridView.问题是我的更改没有显示.
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.
使用调试器,我已经在DGV和BindingSource中查看了正确的数据.它只是不显示在DGV中.
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:
对于解决方法的任何帮助或建议或想法,我们深表感谢.非常感谢您查看此内容.
I appreciate any help or suggestions or ideas for workarounds. Thank you so much for looking at this.
*****************编辑*******************
***************** EDIT *******************
这是保存按钮的代码-我知道它正在工作,因为在我重新查询数据之后,它在绑定源和DGV中.此代码采用单独的添加/编辑形式:
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");
}
}
以下是带有DGV的用户控件的完整代码:
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();
推荐答案
我终于弄明白了这件事.在我的编辑按钮单击"方法中,我需要再次调用用户控件填充表适配器方法.
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.
这篇关于绑定源正确时,DataGridView无法在C#中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!