C# dgv右击数据跳出菜单“测试过程”点击测试过程跳转新窗体progressform并在新窗体中通过获取dgv中的数据找出相应的数据并显示在新窗体的dgv2中
时间: 2024-03-01 16:54:16 浏览: 20
好的,我可以给你提供C#代码实现右击DataGridView中的数据项并在弹出的菜单中点击“测试过程”后跳转到新窗体ProgressForm,并在新窗体中获取DataGridView中的数据,找出相应的数据,并显示在新窗体的DataGridView2中。以下是示例代码:
1. 在DataGridView上添加右键菜单:
```csharp
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenuStrip menu = new ContextMenuStrip();
menu.Items.Add("测试过程", null, TestProcess_Click);
dataGridView1.ContextMenuStrip = menu;
}
}
private void TestProcess_Click(object sender, EventArgs e)
{
// 获取选中的行数据
DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
string data = selectedRow.Cells[0].Value.ToString();
// 打开新窗体并传递数据
ProgressForm progressForm = new ProgressForm(data);
progressForm.Show();
}
```
2. 在ProgressForm中添加构造函数,接收并显示传递的数据,并根据数据查找相应的数据项:
```csharp
public partial class ProgressForm : Form
{
private string data;
public ProgressForm(string data)
{
InitializeComponent();
this.data = data;
LoadData();
}
private void LoadData()
{
// 在DataGridView2中查找相应的数据项
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (row.Cells[0].Value.ToString() == data)
{
dataGridView2.ClearSelection();
row.Selected = true;
break;
}
}
}
}
```
这段代码将在DataGridView上右键单击后弹出一个菜单,其中包含一个“测试过程”选项。当用户单击该选项时,将获取选中的行数据,然后打开一个新的ProgressForm窗体,并将选中的数据传递给它。在ProgressForm中,数据将被保存在一个私有字段中,并在LoadData方法中查找相应的数据项,并在DataGridView2中选中它。你可以根据需要自定义DataGridView2的列和样式。