gpt4 book ai didi

c# - 从其他窗体更改 DataGridView 的单元格的值

转载 作者:行者123 更新时间:2023-11-30 17:37:29 27 4
gpt4 key购买 nike

我有两种形式:

  1. Form1 包含 DataGridView 控件

  2. Form2 包含 Textbox 控件(处于只读模式)、checkBox 和 Button。

当我选择 DataGridView 行时,它将显示 Form2 并在 TextBoxes 中显示它们的值。现在一切似乎都好起来了。我想知道的是在文本框中显示数据后,我选中 RadioButton,然后单击该按钮,它将返回到所选行的 Form1 并自动更改 Cell 4 的值

这是我的代码:

表格 1

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,否则设置为取消:

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

Form1 中,使用 ShowDialog 而不是 Show 并检查结果是否为 OK,执行更新你需要的:

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

这样每个表单都有自己的责任,你把与Form1相关的代码写在Form1中,你不需要在中写它们表格 2

关于c# - 从其他窗体更改 DataGridView 的单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38011808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com