C# 从其他usercontrol调用Update GridControl时不显示更改
我已经创建了一个方法来更新我的GridControl。对于我的另一个GridControl,我使用相同的方法/构造函数结构,并以相同的方式调用它,它可以工作。当我单击与update方法在同一类中的按钮时,网格中的数据被更新。但是当我从另一个usercontrol调用此方法updateGridControl时,GridControl不会发生任何变化。当我从另一个usercontrol/类调用网格时,如何更改我的代码以使该方法也更新网格 这是对方法的调用:C# 从其他usercontrol调用Update GridControl时不显示更改,c#,C#,我已经创建了一个方法来更新我的GridControl。对于我的另一个GridControl,我使用相同的方法/构造函数结构,并以相同的方式调用它,它可以工作。当我单击与update方法在同一类中的按钮时,网格中的数据被更新。但是当我从另一个usercontrol调用此方法updateGridControl时,GridControl不会发生任何变化。当我从另一个usercontrol/类调用网格时,如何更改我的代码以使该方法也更新网格 这是对方法的调用: incidentCategorySearc
incidentCategorySearchControl.Instance.updateGridControl(); // update doesn't work
incidentCategorySearchControl.cs
public partial class incidentCategorySearchControl : UserControl
{
public static incidentCategorySearchControl Instance { get; set; }
public incidentCategorySearchControl()
{
InitializeComponent();
Instance = this;
// This line of code is generated by Data Source Configuration Wizard
incIncidentCategoryTableAdapter1.Fill(reinvenT_QualityDataSet1.IncIncidentCategory);
bindingSource1.DataSource = reinvenT_QualityDataSet1;
}
public void updateGridControl()
{
incIncidentCategoryTableAdapter1.Fill(reinvenT_QualityDataSet1.IncIncidentCategory);
}
/// <summary>
/// GridControl row doubleclick handling
/// </summary>
/// <param name="view"></param>
/// <param name="pt"></param>
public void DoRowDoubleClick(GridView view, Point pt)
{
GridHitInfo info = view.CalcHitInfo(pt);
if (info.InRow || info.InRowCell)
{
try
{
string colCaption = info.Column == null ? "N/A" : info.Column.GetCaption();
Form1 myform = (Form1)this.categoryGridControl.FindForm();
int id = Convert.ToInt32(view.GetRowCellValue(info.RowHandle, "IncIncidentCategory"));
string description=view.GetRowCellValue(info.RowHandle,"IncIncidentCatDescription").ToString();
IncidentCategory incidentCategoryEditTarget = new IncidentCategory(id, description);
incidentCategoryChange.Instance.TextBoxCategory = incidentCategoryEditTarget._categoryId.ToString();
incidentCategoryChange.Instance.TextBoxCategoryDesc = incidentCategoryEditTarget._description;
XtraTabPage xtraTabPage = myform.xtraTabControl1.TabPages[3];
xtraTabPage.PageVisible = true;
myform.xtraTabControl1.SelectedTabPage = xtraTabPage;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
/// <summary>
/// CategoryGridControl double click event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void categoryGridControl_DoubleClick(object sender, EventArgs e)
{
GridView view = (GridView)categoryGridControl.MainView;
Point pt = view.GridControl.PointToClient(Control.MousePosition);
DoRowDoubleClick(view, pt);
}
private void closeButton_Click(object sender, EventArgs e)
{
Form1 myform = (Form1)this.closeButton.FindForm();
myform.xtraTabControl1.SelectedTabPage.PageVisible = false;
}
private void addCategoryButton_Click_1(object sender, EventArgs e)
{
Form1 myform = (Form1)this.addCategoryButton.FindForm();
XtraTabPage xtraTabPage = myform.xtraTabControl1.TabPages[2];
xtraTabPage.PageVisible = true;
myform.xtraTabControl1.SelectedTabPage = xtraTabPage;
}
private void simpleButton1_Click(object sender, EventArgs e)
{
updateGridControl();
}
}
意外类别更改.cs
namespace REINVENT.Quality
{
public partial class incidentCategoryChange : UserControl
{
public static incidentCategoryChange Instance { get; set; }
SqlCommand cmd = new SqlCommand();
public incidentCategoryChange()
{
InitializeComponent();
Instance = this;
// set no blinking
errorProvider1.BlinkStyle = ErrorBlinkStyle.NeverBlink;
}
//public property get/set incidentcategory
public string TextBoxCategory
{
get { return incidentCategoryTextBox.Text; }
set { incidentCategoryTextBox.Text = value; }
}
//public property get/set incidentcategory description
public string TextBoxCategoryDesc
{
get { return incidentCategoryDescTextBox.Text; }
set { incidentCategoryDescTextBox.Text = value; }
}
/// <summary>
/// Check if a control contains text
/// </summary>
/// <param name="control"><Control</param>
public void controlContainsText(Control control)
{
string errorMessage = "Dit veld mag niet leeg zijn";
if (control.Text.Trim().Length == 0)
errorProvider1.SetError(control, errorMessage);
else
errorProvider1.SetError(control, "");
}
private void closeCategoryChangeButton_Click(object sender, EventArgs e)
{
Form1 myform = (Form1)this.closeCategoryChangeButton.FindForm();
incidentCategoryDescTextBox.Enabled = true;
myform.xtraTabControl1.SelectedTabPage.PageVisible = false;
}
/// <summary>
/// Save the category to the database if the required fields are filled and update grids
/// </summary>
/// <param name="sender">sender object</param>
/// <param name="e">event</param>
private void saveCategoryChangeButton_Click(object sender, EventArgs e)
{
if (incidentCategoryDescTextBox.Text.Trim().Length == 0)
{
MessageBox.Show("Opslaan mislukt controleer a.u.b. of alle verplichte velden juist zijn ingevuld");
}
else
{
try
{
using (SqlConnection con = new SqlConnection(SqlServerConnection.sqlConnection.ConnectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("UPDATE IncIncidentCategory SET IncIncidentCatDescription = @categoryDescription WHERE IncIncidentCategory = @categoryId", con))
{
IncidentCategory incidentCategoryUpdate = new IncidentCategory(Convert.ToInt32(incidentCategoryTextBox.Text), incidentCategoryDescTextBox.Text);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@categoryId", incidentCategoryUpdate._categoryId);
cmd.Parameters.AddWithValue("@categoryDescription", incidentCategoryUpdate._description);
cmd.ExecuteNonQuery();
MessageBox.Show("Wijzigingen opgeslagen");
}
}
incidentTypeChange.Instance.refreshCategorySearchBox();
incidentTypeMaintenance.Instance.refreshCategorySearchBox();
incidentCategorySearchControl.Instance.updateGridControl(); // update doesn't work
}
catch
{
MessageBox.Show("Voer voor beide velden geldige waarden in a.u.b.");
}
}
}
/// <summary>
/// Delete a category after confirmation of the user and update grids.
/// </summary>
/// <param name="sender">object sender</param>
/// <param name="e">event</param>
private void deleteCategoryButton_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Weet u zeker dat u deze categorie wilt verwijderen?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
using (SqlConnection con = new SqlConnection(SqlServerConnection.sqlConnection.ConnectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("DELETE FROM IncIncidentCategory WHERE IncIncidentCategory = @idDelete", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@idDelete", Convert.ToInt32(incidentCategoryTextBox.Text));
cmd.ExecuteNonQuery();
}
}
incidentCategoryTextBox.Text = "";
incidentCategoryDescTextBox.Text = "";
MessageBox.Show("De categorie is verwijderd");
incidentTypeChange.Instance.refreshCategorySearchBox();
incidentTypeMaintenance.Instance.refreshCategorySearchBox();
}
catch
{
MessageBox.Show("Voer voor beide velden geldige waarden in a.u.b. Verwijzingen naar deze catergorie dienen eerst verwijderd/aangepast te worden");
}
}
}
private void incidentCategoryDescTextBox_Validating(object sender, CancelEventArgs e)
{
controlContainsText(incidentCategoryDescTextBox);
}
private void newCategoryButton_Click(object sender, EventArgs e)
{
Form1 myform = (Form1)this.newCategoryButton.FindForm();
XtraTabPage xtraTabPage = myform.xtraTabControl1.TabPages[2];
xtraTabPage.PageVisible = true;
myform.xtraTabControl1.SelectedTabPage = xtraTabPage;
}
}
}
什么意味着更新不起作用?是否设置了断点?你有例外吗?结果不是预期的吗?若通过其他方式调用updateGridControl,结果是否正确?当从其他usercontrol调用时,你如何得出更新GridControl不显示更改的结论?当我运行我的程序并在这一行上设置断点时,更新方法中的行就到达了。我没有任何例外。GridControl显示数据库中最新值的预期结果。当我按下simpleButton1时,该方法将工作一段时间,GridControl将更新为数据库中的最新值。但是,当我从我的其他用户控件调用update方法时,值不会被更新。除非您使用多个incidentCategorySearchControls,否则您的方法应该有效。@Sinatr非常感谢这就是问题所在,另一个仍然隐藏在我的窗体上,问题已解决。