question

ravikumar-1532 avatar image
0 Votes"
ravikumar-1532 asked AbdulhakimMElrhumi-5836 answered

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

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
.

[1]: https://drive.google.com/file/d/1c6_8zB9OQMObLBNIj3e5L40PhFXCeuFZ/view?usp=sharing

dotnet-csharpvs-generalwindows-forms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@karenpayneoregon can you please help me on this .... by completing this my app will get finished ..so i need your help badly..

0 Votes 0 ·

Hi
FrmEditBank frm = new FrmEditBank();
frm.txtBankId.Text = gvBankCard.GetFocusedRowCellValue("BankId").ToString();
frm.cmbBankName.Text = gvBankCard.GetFocusedRowCellValue("BankName").ToString();
frm.ShowDialog();

Best Regards.

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

0 Votes 0 ·
cooldadtx avatar image
0 Votes"
cooldadtx answered

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      
}
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ravikumar-1532 commented

Hello @ravikumar-1532

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.



a1.png (43.5 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you much ...it was very helpful for me..

0 Votes 0 ·
KyleWang-MSFT avatar image
1 Vote"
KyleWang-MSFT answered KyleWang-MSFT edited

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();
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AbdulhakimMElrhumi-5836 avatar image
0 Votes"
AbdulhakimMElrhumi-5836 answered AbdulhakimMElrhumi-5836 edited

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.