如何将datagridview的单元格的vaule传递给另一种形式 [英] How to pass vaule of cell of datagridview to another form

查看:75
本文介绍了如何将datagridview的单元格的vaule传递给另一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种形式。第一种形式有一个文本框和一个图片框。当点击图片框时,它会打开第二个包含datagridview的表单。我想要的是当我点击datagridview的单元格时它的值应该显示在第一个表单的文本框中。我不想以第二种形式创建第一个表单的对象,因为网格视图的数据必须传递给多个表单



我尝试了什么:



我创建了第二种形式的第一种形式的对象,但这不是我想要的方式

There are two forms.In first form there is a textbox and a picture box.When picture box is clicked it opens the second form which contains a datagridview.What i want is when i clicked on the cell of a datagridview its value should be shown in the textbox of first form.I do not want to create object of first form in second form because data of grid views has to be passed to multiple forms

What I have tried:

I have created an object of first form in second form but this is not the way i want

推荐答案

这简化为与表单协作相关的问题;由于某种原因,一个非常受欢迎的。



由于我之前对这个主题的回答经常不太了解,可能还不够清楚,我决定写一个提示/ Trick文章完整的详细代码示例和解释:一次回答的许多问题 - Windows窗体或WPF Windows之间的协作



-SA
This is reduced to the question related to form collaboration; by some reason, a very popular one.

As my previous answers on the topic often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA


尝试这个



Form2 中创建 TextBox 属性为

Try this

Create a TextBox property in Form2 as
public partial class Form2 :Form 
{
public TextBox ParentFormTextBox { get; set; }
public Form2()
.
.



,使用Current(Form1)表单 TextBox 初始化上述属性as


before showing the Form2, initialize the above property with the Current (Form1) Form's TextBox as

Form2 objForm2 = new Form2();
objForm2.ParentFormTextBox = this.textBox_Whatever;
objForm2.Show();



并使用<$ c $将datagridview单元格的值分配给父表单的 TextBox c> CellClick 事件为




and assign the values of the datagridview cell to the Parent Form's TextBox using CellClick event as

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
      {
          if (e.RowIndex > -1 && e.ColumnIndex > -1)
          {
             string value = Convert.ToString( dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
             ParentFormTextBox.Text = value;
          }
      }


这篇关于如何将datagridview的单元格的vaule传递给另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆