在GridView(Form1)中选择一条记录,它显示在文本框(Form2)中 [英] Select one record in GridView (Form1) and it show display in Text Box(Form2)

查看:80
本文介绍了在GridView(Form1)中选择一条记录,它显示在文本框(Form2)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友



我在C#GUI WIN应用程序中拖拽Form form1和form2。



在表单1中我有一个dataGridview,在form2中我有一个文本框。我希望当我在网格视图中选择一行时,它应该以其他形式显示在文本框中。请帮助。

Dear Friends

I have tow Form form1 and form2 in C# GUI WIN application.

In form 1 i have a dataGridview, and in form2 i have a text box. I want that when i select a row in grid view it shoud be show in text box wich is in other form .Please help.

推荐答案

首先存储griedview一个表中的值,并在for2中创建一个全局变量并访问该表值....然后在表单2中使用for循环表记录.....并在form2中访问
first of all store the griedview value in one table and create a one globally variable in for2 and accesing that table values....then in form 2 using for loop that table record.....and accessing in form2


好的,我将引导您完成一个可能的(注意 - 可能而不一定是最好的)解决方案,因为我不喜欢已经发布的解决方案。



你需要考虑的几件事情......显示第二种形式的是什么,以及如何记录它的实例。



我创建了一个简单的WinForm应用程序,它有两种形式 - Form1和Form2。



Form1 有一个DataGridView( dataGridView1 )出于此目的,有一个填充数字0到99的列。



Form2 有一个TextBox ( textBox2



我正在使用 SelectionChanged DataGridView上的事件以第二种形式处理显示。



这是我的代码 - 我试图添加适当的注释来解释我的思考。

Ok I''m going to walk you through a possible (note - possible and not necessarily the best) solution because I don''t like the one that has already been posted.

A couple of things you have to consider ... what is displaying the second form and how do you keep a note of it''s instance.

I''ve created a simple WinForm application that has two forms - Form1 and Form2.

Form1 has a DataGridView (dataGridView1) which for the purposes of this has a single column populated with numbers 0 to 99.

Form2 has a single TextBox (textBox2)

I''m using the SelectionChanged event on the DataGridView to handle the display in the second form.

Here''s my code - I''ve tried to add appropriate comments to explain my thinking.
using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        // Declare the second form at the class level of Form1
        private Form2 f2 = new Form2();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // DEMO ONLY
            // Put some data into the datagridview
            for (int i = 0; i < 100; i++)
                dataGridView1.Rows.Add(i.ToString());
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (f2.Visible == false)
                f2.Show();  // make sure form2 is visible

            // Find the textbox on form2
            TextBox t = (TextBox)f2.Controls.Find("textBox2", true)[0];

            // Populate the textbox with some data from the grid
            t.Text = dataGridView1.SelectedCells[0].Value.ToString();

        }
    }
}





如果你想研究其他方式(并且你应该尝试从这里开始代码项目文章 [ ^ ]


这篇关于在GridView(Form1)中选择一条记录,它显示在文本框(Form2)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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