【问题标题】:Refresh DataGridView in usercontrol from other usercontrol after changes更改后从其他用户控件刷新用户控件中的 DataGridView
【发布时间】:2014-10-14 11:40:07
【问题描述】:

我的用户控件中有一个 DataGridView,我想在进行更改时刷新它。例如,当我单击其他用户控件中的保存按钮时,应调用带有 DataGridView 的用户控件中的刷新功能。我已经搜索了这个问题的解决方案,但没有一个能解决我的问题(我尝试了 ResetBindings,将 DataSource 设置为 null 并再次设置 DataSource 等)。要从另一个用户控件调用用户控件,我使用实例。

   public partial class incidentTypeSearchControl : UserControl
{
    public static incidentTypeSearchControl Instance { get; set; }
    REINVENT_QualityDataSet reinventQualityDataset = new REINVENT_QualityDataSet();
    SqlCommand cmd = new SqlCommand();

    BindingSource source = new BindingSource();
    REINVENT_QualityDataSetTableAdapters.IncIncidentTypeTableAdapter incidentTypeAdapter =
        new REINVENT_QualityDataSetTableAdapters.IncIncidentTypeTableAdapter();

    public incidentTypeSearchControl()
    {
        InitializeComponent();
        Instance = this;
        this.dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellDoubleClick);
    }

    private void incidentTypeSearchControl_Load(object sender, EventArgs e)
    {                   
        incidentTypeAdapter.Fill(reinventQualityDataset.IncIncidentType);

        source.DataSource = reinventQualityDataset.IncIncidentType;

        dataGridView1.AutoGenerateColumns = true;           
        //dataGridView1.DataSource = reinventQualityDataset.IncIncidentType;
        dataGridView1.DataSource = source;
        dataGridView1.Columns[0].HeaderText = "Id";
        dataGridView1.Columns[1].HeaderText = "Incidenttype";
        dataGridView1.Columns[2].HeaderText = "Categorie";
        dataGridView1.Columns[3].HeaderText = "Tellen in statistiek";
        dataGridView1.Columns[4].HeaderText = "Punten";            
        dataGridView1.Columns[5].HeaderText = "Faalkosten";
        dataGridView1.Columns[6].HeaderText = "Kostendrager";

        for (int i = 0; i < 6; i++)
        {
            dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }          
        dataGridView1.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

        dataGridView1.BackgroundColor = System.Drawing.SystemColors.Control;

    }
    //added/      
    public DataGridView myDataGrid
    {
        get { return dataGridView1; }
        set { dataGridView1 = value; }
    }

    public void updateDataGridView()
    {
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = source;
        source.ResetBindings(true);
    }

在其他用户控件中,我可以像这样访问 datagridview:

    incidentTypeSearchControl.Instance.myDataGrid // access datagridview

调用 updateGridView() 方法会将网格标题文本设置回默认值(这不是本意),并且不会更新 datagridview 中的值。如何制作一个 刷新/更新我的 datagridview 并从我的其他用户控件调用此方法的方法?

更新:

问题通过再次填充数据集解决

【问题讨论】:

    标签: c# datagridview user-controls


    【解决方案1】:

    试试

    dataGridView1.DataSource = typeof(IncIncidentTypeDataTable); // or whatever type is your source
    dataGridView1.DataSource = source;
    

    【讨论】:

    • 这会将标题文本设置为默认值,但不会更新值。
    • 我使用 updateGridView() 方法更新值。目前,此方法将列标题文本设置为默认值并且不更新任何内容。那么我该如何解决呢?
    • 这很奇怪,如果您使用的是 BindingSource,则根本不需要重置 datagridview 的 DataSource。如果您有一个作为 BindingSource 的 DataSource 的 DataTable,那么它就是您的 GridView 的 DataSource,那么您对基础 DataTable 所做的所有更改都应该传播到 DGV,最终 bindingSource.ResetBindings(false) 或 bnindingSOurce.EndEdit() 应该是做这个把戏......
    • 我通过再次填充数据集解决了我的问题。无论如何谢谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多