我正在更新表格中的一行。表的一个子集显示在DataGridView中。当我更新行时,更改不会反映在DataGridView中。即使我在打电话 提交更改后,DataGridView.Invalidate()和DataGridView.Refresh(),我必须关闭应用程序,重新启动,并重新运行查询,然后才能看到更改。
相关代码是:
private void buttonUpdate_Click(object sender, EventArgs e)
{
const int TICKETID_COLUMN = 0;
String _ticketID = dataGridView1.CurrentRow.Cells[SOME_COLUMN].Value.ToString();
UpdateRecord(_ticketID, textBoxTicketSource.Text,
textBoxAboutSomeID.Text, textBoxCategoryID.Text, textBoxContactEmail.Text);
}
private void UpdateRecord(string ATicketID, string ATicketSource, string
AAboutSomeID, string ACategoryID, string AContactID)
{
oracleConnection1.Open();
OracleCommand ocmd = new OracleCommand();
OracleTransaction ot;
// Start a local transaction
ot = oracleConnection1.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
// Assign transaction object for a pending local transaction
ocmd.Transaction = ot;
ocmd.Connection = oracleConnection1;
try
{
ocmd.CommandText = @"UPDATE ABC.CONCERTTICKETS
SET TICKETSOURCE = :p_TICKETSOURCE,
ABOUTSOMEID = :p_ABOUTSOMEID,
CATEGORYID = :p_CATEGORYID,
CONTACTEMAIL = :p_CONTACTEMAIL
WHERE TICKETID = :p_TICKETID";
ocmd.Parameters.Add("p_TICKETSOURCE", ATicketSource);
ocmd.Parameters.Add("p_ABOUTSOMEID", Convert.ToInt32(AAboutSOMEID));
ocmd.Parameters.Add("p_CATEGORYID", Convert.ToInt32(ACategoryID));
ocmd.Parameters.Add("p_CONTACTEMAIL", AContactID);
ocmd.Parameters.Add("p_TICKETID", ATicketID);
ocmd.ExecuteNonQuery();
ot.Commit();
Popul8TheGrid();
dataGridView1.Invalidate();
dataGridView1.Refresh();
}
catch (Exception e)
{
ot.Rollback();
throw;
}
finally
{
oracleConnection1.Close();
}
}
private void Popul8TheGrid()
{
int iFromYear = dateTimePickerFrom.Value.Year;
int iFromMonth = dateTimePickerFrom.Value.Month;
int iFromDay = dateTimePickerFrom.Value.Day;
int iToYear = dateTimePickerTo.Value.Year;
int iToMonth = dateTimePickerTo.Value.Month;
int iToDay = dateTimePickerTo.Value.Day;
oracleCommand1.Parameters.Clear();
oracleCommand1.Parameters.Add("iStartDate", new DateTime(iFromYear, iFromMonth,
iFromDay));
oracleCommand1.Parameters.Add("iEndDate", new DateTime(iToYear, iToMonth,
iToDay));
oracleCommand1.Parameters.Add("iCATEGORYID", 114);
// OracleRef is apparently like OracleDbType.RefCursor;
OracleRef or = new OracleRef("_or");
oracleCommand1.Parameters.Add("cref", or);
oracleConnection1.Open();
oracleDataAdapter1.SelectCommand = oracleCommand1;
oracleDataAdapter1.GetFillParameters();
oracleDataAdapter1.Fill(oracleDataTable1);
dataGridView1.DataSource = oracleDataTable1;
oracleConnection1.Close();
}
更新:
基于霍尔的建议(我试图回复评论,但似乎挂了):
好的,我现在有了这个:
oracleDataAdapter1.SelectCommand = oracleCommand1;
oracleDataAdapter1.GetFillParameters();
oracleDataAdapter1.Fill(oracleDataTable1);
// I don't see a "Clear" method or some such...
dataGridView1.DataSource = null;
//dataGridView1.DataSource = oracleDataTable1;
BindingSource b = new BindingSource();
b.DataSource = oracleDataTable1;
dataGridView1.DataSource = b;
b.ResetBindings(false);
oracleConnection1.Close();
...它仍然可以正常工作 - 更新,但DataGridView在重新启动应用程序之前不知道它。
答案 0 :(得分:5)
Invalidate()
和Refresh()
不重新查询数据源的原因是它们仅用于图形方面 - 它们都使控件的客户区无效并强制执行重新绘制,但问题是底层控件认为其数据源中没有任何变化,因为它依赖于数据源来告诉它何时发生。
您需要的是DataSource
能够告诉DataGridView
BindingList<T>
或BindingSource
发生了什么,两者都有ListChanged
订阅的DataGridView
事件。
我原以为DataTable
在更改时也通知了网格,但我错了或OracleDataTable
不同。
应该解决的问题是引入BindingSource
并将其作为DataGridView
的数据源。然后将OracleDataTable
设置为绑定源的数据源。如果这不起作用,则可以在绑定源上调用ResetBindings()
方法。
BindingSource b = new BindingSource();
b.DataSource = oracleDataTable1;
dataGridView1.DataSource = b;
答案 1 :(得分:1)
尝试做:
dataGridView1.DataSource = null;
dataGridView1.DataSource = oracleDataTable1;
答案 2 :(得分:-2)
dataGridView1.DataSource = null;
dataGridView1.DataBind();
dataGridView1.DataSource = oracleDataTable1;
dataGridView1.DataBind();