How do I pass DataGridView selected row value to TextBox in another form?

ravi kumar 331 Reputation points
2020-12-21T05:44:34.287+00:00

hello all ,

i am very new to c#

I am creating a small data entry application using WinForms and MSSQL database ,

In this i need to pass Datagridview selected row value to the textbox , combo box in another form?

the datagridview form: "FormDataEntry" from this when the user clicks the edit button the data should get populated to "FormEdit" and user can modify and save the same

I have tried many ways but can't get the expected result , by completing this my app will get finished , kindly help me in this regard.
below link the address to my "sln" fie , kindly go through the same for reference
.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,837 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,631 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,291 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points
    2020-12-22T05:25:35.767+00:00

    A simple way to pass value to another form is that you can define a public property in "another form" and then assign it after calling "Show()".

    // AnotherForm.cs
    public TextBox TB
    {
        get { return textBoxinAnotherForm; }
        set{ textBoxinAnotherForm = value; }
    }
    
    //Form1.cs
    private void btnTransfer_Click(object sender, EventArgs e)
    {
        AnotherForm anotherForm = new AnotherForm();
        anotherForm.Show();
        anotherForm.TB.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
    }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Michael Taylor 48,656 Reputation points
    2020-12-21T16:23:18.74+00:00

    Passing control data to another form is really never the right answer. This too tightly couples your app together. There are better solutions.

    The best solution, in my opinion, is to pass the actual data you're editing, forget the DGV row. I assume that in order to get the DGV to populate you loaded data from the DB. The DB returned to you either a Dataset or your own custom business objects. Then you set that as the Datasource for the DGV. When the user interacts with the DGV the event that is raised gives you access to the data source data for that row. Pass the DataRow or your custom business object that is associated with that row to the other form to edit it.

    csharp
    private void LoadData ()
    {
       var movies = GetMoviesFromDatabase();
       datagridview1.DataSource = movies;
    }
    
    //Some event handler that is called when you want to edit the row
    private void OnEditRow ( object sender, DataGridViewCellEventArgs e )
    {
       var dvg = sender as DataGridView;
    
       //Get the current row's data, if any
       var row = dvg.Rows[e.RowIndex];
    
       //This works if you data bound the DGV as normal
       var movie = row.DataBoundItem as Movie;  //Or DataRow if you're using a Dataset
       if (movie != null)  //Could be a header row...
           //Edit movie      
    }
    
    0 comments No comments

  3. Karen Payne MVP 35,196 Reputation points
    2020-12-21T20:05:59.15+00:00

    Hello @ravi kumar

    I have a code sample on GitHub that has more than asked for but does cover what you want, in this case via a button in a BindingNavigator which could be done in a regular Button or several other means.

    GitHub repository

    https://github.com/karenpayneoregon/MasterDetailsSqlServerNorthWind

    Screenshot

    50085-a1.png

    Note

    In the future if possible use a GitHub repository to provide code rather than Google drive.


  4. Abdulhakim M. Elrhumi 351 Reputation points
    2020-12-23T08:35:04.543+00:00

    Hi

    private void btnUpdate_Click(object sender, EventArgs e)
    {
    Frm_Update_Product frm = new Frm_Update_Product();
    frm.cmbCategories.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
    frm.txtRef.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
    frm.txtDes.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
    frm.txtQty.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
    frm.txtPrice.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();

            frm.ShowDialog();
        }
    

    Best Regards.

    Please click the Mark as answer button and vote as helpful if this reply solves your problem.

    0 comments No comments