C# 为什么我的DataGridview拒绝刷新?
我正在更新表中的一行。表的子集显示在DataGridView中。更新行时,更改不会反映在DataGridView中。即使我在打电话 DataGridView.Invalidate()和DataGridView.Refresh()提交更改后,我必须关闭应用程序,重新启动,然后重新运行查询,才能看到更改 相关代码为:C# 为什么我的DataGridview拒绝刷新?,c#,.net,oracle,datagridview,dotconnect,C#,.net,Oracle,Datagridview,Dotconnect,我正在更新表中的一行。表的子集显示在DataGridView中。更新行时,更改不会反映在DataGridView中。即使我在打电话 DataGridView.Invalidate()和DataGridView.Refresh()提交更改后,我必须关闭应用程序,重新启动,然后重新运行查询,才能看到更改 相关代码为: private void buttonUpdate_Click(object sender, EventArgs e) { const int TICKETID_COLUMN
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直到我重新启动应用程序才知道它。尝试执行以下操作:
dataGridView1.DataSource = null;
dataGridView1.DataSource = oracleDataTable1;
试着做:
dataGridView1.DataSource = null;
dataGridView1.DataSource = oracleDataTable1;
原因是无效(和刷新()不要重新查询数据源,因为它们只用于处理图形方面的内容-它们都会使控件的客户端区域无效并强制重新绘制,但问题是底层控件认为其数据源中没有任何更改,因为它依赖于数据源来告诉它何时更改发生了 你需要的是你的
DataSource
能够告诉DataGridView
发生了什么,比如bindingslist
或BindingSource
,它们都有DataGridView
订阅的ListChanged
事件
我原以为DataTable
在网格更改时也会通知网格,但我不是搞错了,就是OracleDataTable
不同了
解决此问题的方法是引入BindingSource
,并将其作为DataGridView
的数据源。然后将您的OracleDataTable
作为绑定源的数据源。如果这不起作用,那么可以调用绑定源上的ResetBindings()
方法
BindingSource b = new BindingSource();
b.DataSource = oracleDataTable1;
dataGridView1.DataSource = b;
原因是无效(和刷新()不要重新查询数据源,因为它们只用于处理图形方面的内容-它们都会使控件的客户端区域无效并强制重新绘制,但问题是底层控件认为其数据源中没有任何更改,因为它依赖于数据源来告诉它何时更改发生了 你需要的是你的
DataSource
能够告诉DataGridView
发生了什么,比如bindingslist
或BindingSource
,它们都有DataGridView
订阅的ListChanged
事件
我原以为DataTable
在网格更改时也会通知网格,但我不是搞错了,就是OracleDataTable
不同了
解决此问题的方法是引入BindingSource
,并将其作为DataGridView
的数据源。然后将您的OracleDataTable
作为绑定源的数据源。如果这不起作用,那么可以调用绑定源上的ResetBindings()
方法
BindingSource b = new BindingSource();
b.DataSource = oracleDataTable1;
dataGridView1.DataSource = b;
试过了;仍然没有“刷新”您是否在BeginTransaction部分执行此操作?它通常对我有用。这里有一个类似的问题:试过了;仍然没有“刷新”您是否在BeginTransaction部分执行此操作?它通常对我有用。这里有一个类似的问题:DataGridView没有DataBind()方法:“System.Windows.Forms.DataGridView”不包含“DataBind”的定义,并且找不到接受“System.Windows.Forms.DataGridView”类型的第一个参数的扩展方法“DataBind”(是否缺少using指令或程序集引用?)DataBind是来自Asp.Net gridview控件的方法,而不是winforms DataGridView。是否尝试了
.Refresh()
或.Update()
?DataGridView没有DataBind()方法:“System.Windows.Forms.DataGridView”不包含“DataBind”的定义,并且找不到接受类型为“System.Windows.Forms.DataGridView”的第一个参数的扩展方法“DataBind”(是否缺少using指令或程序集引用?),不是winforms DataGridView。您是否尝试过.Refresh()
或.Update()
?因此您实际上将网格的数据源设置为null,每次创建一个全新的bindingsource,将绑定源绑定到DataGridView,然后重置绑定,而网格仍然不显示新数据?您的代码中出现了一些非常奇怪的情况,因为您已经完成的步骤实际上是多余的-您不应该需要一半的步骤来实现这一点。如果使用调试器,您会在datatable中看到什么?如果将绑定源升级到类级变量,然后只更新数据表,而不是完全重新创建它,会发生什么情况?因此,您实际上将网格的数据源设置为null,每次创建一个全新的绑定源,将绑定源绑定到datagridview,然后重置绑定,你的网格还没有显示新数据吗?您的代码中出现了一些非常奇怪的情况,因为您已经完成的步骤实际上是多余的-您不应该需要一半的步骤来实现这一点。如果使用调试器,您会在datatable中看到什么?如果将绑定源升级到类级变量,然后只更新数据表而不是完全重新创建它,会发生什么?