[英]Change the Value of Cell of DataGridView from other Form
I have 2 forms: 我有两种形式:
Form1
contains DataGridView
controls Form1
包含DataGridView
控件
Form2
contains Textbox
controls (are in mode read only), checkBox and Button. Form2
包含Textbox
控件(模式只读),checkBox和Button。
When I select a DataGridView
Row it will show me Form2 and display their values in TextBoxes. 当我选择一个
DataGridView
行时,它将向我显示Form2并在TextBoxes中显示它们的值。 Everything seems better just now. 刚才一切似乎都好一些。 What I want know is after displaying data in textboxes, I check the RadioButton then click the button it will return to Form1 of the selected Row and Change the value of Cell 4 automatically.
我想知道的是在文本框中显示数据后,我检查RadioButton然后单击它将返回到所选行的Form1的按钮并自动更改单元格4的值 。
Here there is My code: 这里有我的代码:
Form1 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();
}
}
Form2 窗体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();
}
}
How Can I do That ? 我怎样才能做到这一点 ?
In the button click event on Form2
, check if the check box is checked, just set DialogResult
to OK
, else set it to Cancel
: 在
Form2
上的按钮单击事件中,检查是否选中了复选框,只需将DialogResult
设置为OK
,否则将其设置为Cancel
:
if (checkBox1.Checked)
this.DialogResult = System.Windows.Forms.DialogResult.OK;
else
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
In Form1
, instead of Show
, use ShowDialog
and check if the result is OK
, perform the update which you need: 在
Form1
,而不是Show
,使用ShowDialog
并检查结果是否OK
,执行您需要的更新:
var f2 = new Form2();
//Set values
if(f2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//Perform update here
}
This way each form has its own responsibility and you write the codes which are related to Form1
in Form1
and you don't need to write them in Form2
. 这样每个表单都有自己的责任,您可以在
Form1
编写与Form1
相关的代码,而不需要在Form2
编写它们。
You could create an event on the sub-form and fire it, as in the example below : 您可以在子表单上创建一个事件并触发它,如下例所示:
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();
}
}
}
Then when you create the sub-form youc an subscribe to the event and register an appropriate handler, eg 然后,当您创建子表单时,您可以订阅该事件并注册适当的处理程序,例如
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();
Or you could leverage INotifyPropertyChanged, or indeed specify an explicit callback delegate, Action or Function. 或者您可以利用INotifyPropertyChanged,或者确实指定显式回调委托,Action或Function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.