Reputation: 859
I have dataGridView
in Form1
and I need to transfer the data that in selected row to another form text box
.
When someone clicks on a row and then clicks on update button...
the following screen will occur with the referenced data.
AddUpdateForm
The following code is for the UPDATE
button
private void updateBtn_Click(object sender, EventArgs e)
{
AddUpdateForm addUpdate = new AddUpdateForm();
addUpdate.ShowDialog();
}
This code is for the click event on dataGridViewPRoducts
private void dataGridViewProducts_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
AddUpdateForm addUpdate = new AddUpdateForm();
Products pro = new Products();
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridViewProducts.Rows[e.RowIndex];
pro.Name = row.Cells["name"].Value.ToString();
pro.Brand = row.Cells["brand"].Value.ToString();
pro.Item_Price = Convert.ToDouble(row.Cells["item_price"].Value.ToString());
pro.StokType =row.Cells["stock_type"].Value.ToString();
pro.Stock_Amount = Convert.ToDouble(row.Cells["stock_amount"].Value.ToString());
textBox1.Text = row.Cells["name"].Value.ToString();
}
Actually I know i need to return the pro
object to the UPDATE button
. However, I don't know how to do it.
Upvotes: 0
Views: 725
Reputation: 9469
From your comment about the two problems…
”First one, AddUpdateForm does not except a pro object.”
This is true, and my comment implied that you should create a constructor that "does" accept a pro object. Then the form can access the passed in Products
object. Something like…
private Products currentProduct;
public AddUpdateForm(Products passedInProduct) {
currentProduct = passedInProduct;
InitializeComponent();
}
private void AddUpdateForm_Load(object sender, EventArgs e) {
// here you can use the passed in Products object called currentProduct
}
Your second Comment…
”The second one is when I move the code, I need a DataGridViewCellEventArgs object which I have a EventArgs in updateBtn_Click.”
This is not true. You do not need the DataGridViewCellEventArgs
object. In its current state, by setting the pro
object in the grids CellContentClick
event means that ONLY if the user “clicks” on a cell then the pro
object gets set… what if the user pressed the down arrow instead of clicking? If the user never clicks a cell, pro
will be null.
You want to grab the currently “selected” row no matter how the user “selected" it. So it makes no sense to grab this row until the user presses the “update” button. Then we simply select the grids CurrentRow
… i.e. … the girds currently selected row.
Therefore, the update button click event may look something like…
private void updateBtn_Click(object sender, EventArgs e) {
DataGridViewRow row = dataGridViewReport.CurrentRow;
Products pro = new Products {
Name = row.Cells["name"].Value.ToString(),
Brand = row.Cells["brand"].Value.ToString(),
Item_Price = Convert.ToDouble(row.Cells["item_price"].Value.ToString()),
StokType = row.Cells["StokType"].Value.ToString(),
Stock_Amount = Convert.ToDouble(row.Cells["Stock_Amount"].Value.ToString())
};
AddUpdateForm addUpdate = new AddUpdateForm(pro);
addUpdate.ShowDialog();
}
Or if the grids data source is a List<Products>
then instead of grabbing the values from the individual cells, you could just get the DataBoundItem
from the row and cast it to a Products
object like...
Products pro = (Products)dataGridViewReport.CurrentRow.DataBoundItem;
I hope this makes sense.
Upvotes: 2