我有一个DataGridView和一个Edit按钮.当我单击“编辑”按钮并更改字段时,我按“保存”,然后再次出现DataGridView.问题是我的更改没有显示.
使用调试器,我查看了DGV和BindingSource,并找到了正确的数据.它只是不显示在DGV中.
这是我的代码-我确实意识到它是半冗余的,但是在这一点上,我已经花了四个小时了,并且愿意尝试任何事情.
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();
我查看了以下问题(以及许多其他问题),并尝试了他们的建议,但均无济于事:
Datagridview not updating correctly
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?
对于任何解决方法的帮助或建议或想法,我们深表感谢.非常感谢您对此的关注.
*****************编辑*******************
这是保存按钮的代码-我知道它是有效的,因为在我重新查询数据后,它在绑定源和DGV中.此代码采用单独的添加/编辑形式:
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的用户控件的完整代码:
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; }
}
}
这是从编辑返回的主窗体上带有用户控件的代码:
uc.FillTableAdapter();
*********编辑->正在打开“编辑”表单的代码:
AddEditISOX aeix = new AddEditISOX("E", currentISOX);
aeix.ShowDialog();
ISOX_Load(sender, e);
ISOX_Load() calls uc.FillTableAdapter();
解决方法:
第一次分配DataSource时,DataGridView会设置绑定.问题是,如果分配的结构与初始分配不同,则后续的DataSource分配将失败,因为绑定现在处于“关闭”状态
您需要重置DataGridView thusly,以便将数据绑定到新的数据. (该链接适用于VB,但您只需要知道调用方法.即使复制/粘贴也可能会过大.)