簡體   English   中英

從其他表單更改DataGridView的單元格的值

[英]Change the Value of Cell of DataGridView from other Form

我有兩種形式:

  1. Form1包含DataGridView控件

  2. Form2包含Textbox控件(模式只讀),checkBox和Button。

當我選擇一個DataGridView行時,它將向我顯示Form2並在TextBoxes中顯示它們的值。 剛才一切似乎都好一些。 我想知道的是在文本框中顯示數據后,我檢查RadioButton然后單擊它將返回到所選行的Form1的按鈕並自動更改單元格4的值

這里有我的代碼:

Form1中

private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)  
   {  
       DataGridViewCell cell = null;  
       foreach (DataGridViewCell selectedCell in dataGridView1.SelectedCells)  
       {  
           cell = selectedCell;  
           break;  
       }  
       if (cell != null)  
       {  
           DataGridViewRow row = cell.OwningRow;  
           string objet = row.Cells[0].Value.ToString();  
           string objectif = row.Cells[1].Value.ToString();  
           string date = row.Cells[2].Value.ToString();  
           string commentaire = row.Cells[3].Value.ToString();  
           string client = row.Cells[5].Value.ToString();  
           string commercial = row.Cells[6].Value.ToString();  

           Détails_RDV détails = new Détails_RDV();  

           détails.obj = objet;  
           détails.objectif = objectif;  
           détails.date = date;  
           détails.comm = commentaire;  
           détails.clt = client;  
           détails.commer = commercial;  

           détails.Show();  

       }  
   }  

窗體2

public partial class Détails_RDV : Form  
{  
    public string obj ;  
    public string objectif;  
    public string date;  
    public string comm;  
    public string clt ;  
    public string commer;  

    public Détails_RDV()  
    {  
        InitializeComponent();  

    }  

    private void Détails_RDV_Load(object sender, EventArgs e)  
    {  

        txtObjet.Text = obj;  
        txtObjectif.Text = objectif;  
        txtDate.Text = date;  
        txtCommerci.Text = commer;  
        txtClient.Text = clt;  
        txtCommentaire.Text = comm;  
    }  

    private void btnValider_Click(object sender, EventArgs e)  
    {  
        if (checkEffectue.Checked == true)  
        {  
           //What should I write here??
             Liste_RDV lrdv = new Liste_RDV();
             lrdv.Show();

        }  
    }  

我怎樣才能做到這一點 ?

Form2上的按鈕單擊事件中,檢查是否選中了復選框,只需將DialogResult設置為OK ,否則將其設置為Cancel

if (checkBox1.Checked)
    this.DialogResult = System.Windows.Forms.DialogResult.OK;
else
    this.DialogResult = System.Windows.Forms.DialogResult.Cancel;        

Form1 ,而不是Show ,使用ShowDialog並檢查結果是否OK ,執行您需要的更新:

var f2 = new Form2();
//Set values
if(f2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    //Perform update here
}

這樣每個表單都有自己的責任,您可以在Form1編寫與Form1相關的代碼,而不需要在Form2編寫它們。

您可以在子表單上創建一個事件並觸發它,如下例所示:

public partial class Détails_RDV : Form
{
    public string obj;
    public string objectif;
    public string date;
    public string comm;
    public string clt;
    public string commer;

    **// Event fired on effectue checkbox change
    public class EffectueEventArgs : EventArgs
    {
        public EffectueEventArgs(bool val)
        {
            Effectue = val;
        }
        public bool Effectue { get; private set; }
    }
    public delegate void EffectueChangeHandler(object src, EffectueEventArgs e);
    public event EffectueChangeHandler OnEffectueChange;**

    public Détails_RDV()
    {
        InitializeComponent();
    }

    private void Détails_RDV_Load(object sender, EventArgs e)
    {
        txtObjet.Text = obj;
        txtObjectif.Text = objectif;
        txtDate.Text = date;
        txtCommerci.Text = commer;
        txtClient.Text = clt;
        txtCommentaire.Text = comm;
    }

    private void btnValider_Click(object sender, EventArgs e)
    {
        if (checkEffectue.Checked == true)
        {
            **// Notify any event listener of change
            if (OnEffectueChange != null)
                OnEffectueChange (this, new EffectueEventArgs(true);**

            this.Close();
        }
    }
}

然后,當您創建子表單時,您可以訂閱該事件並注冊適當的處理程序,例如

        Détails_RDV détails = new Détails_RDV();
        détails.obj = objet;
        détails.objectif = objectif;
        détails.date = date;
        détails.comm = commentaire;
        détails.clt = client;
        détails.commer = commercial;
        **détails.OnEffectueChange += (src, e) => { row.Cells[4].Value = e.Effectue; };**
        détails.Show();  

或者您可以利用INotifyPropertyChanged,或者確實指定顯式回調委托,Action或Function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM