![](/img/trans.png)
[英]How to change value of one cell onchange of other cell value in DataGridview
[英]Change the Value of Cell of DataGridView from other Form
我有兩種形式:
Form1
包含DataGridView
控件
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.