Click here to Skip to main content
15,917,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
CHill60 1-Mar-13 6:00am    
What have you tried so far? What problems are you having?
Nand Kishor Upadhyay 1-Mar-13 6:08am    
i want to select record from datagrid view and it show in text box....
CHill60 1-Mar-13 6:29am    
Yes - I get that. But what have YOU actually tried to do so far? You have to at least make an attempt at doing your homework before anyone will help
Member 8973214 1-Mar-13 6:49am    
Right CHill.

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
 
Share this answer
 
Comments
CHill60 1-Mar-13 9:55am    
global variable ... hm ... not the way I would have done it
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.
C#
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();

        }
    }
}


If you want to research other ways (and you should) try starting here with this code project article[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900