Reputation: 357
I'll try to be clear :)
I am creating new DataGridView
dynamically thanks to a button.
It works fine, but then I want to be able to change the value of one cell of a DataGridView
and update my database.
The problem is that I do not find how to get the current focused DataGridView
.
I tried to play with the code in this link, I get the name of the current DataGridView
.
But I don't think I can manipulate a control as a DataGridView
.
Here is my function :
private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView datagridcell = new DataGridView();
string codearticle = "'" + CodeArticle0.Text + "'";
string division = "'" + Division0.Text + "'";
string colonne = datagridcell.Columns[e.ColumnIndex].HeaderText;
string valeur = sender.ToString();
string Ligne = datagridcell.Rows[0].Cells[3].ToString();
Controller.ControllerMenuPrincipal.updateQALigneFab(codearticle, division,
colonne, valeur, Ligne);
}
Instead of this :
DataGridView datagridcell = new DataGridView();
I want to access the current/focused datagridview
I do not know if it is possible, I am a noob in winforms.
thanks for the help
Upvotes: 1
Views: 259
Reputation: 357
Solution As Reza Aghaei said,
" If you attached dataGridView_CellValueChanged to CellValueChanged of yourdynamic grids, then the sender parameter is the DataGridView which the event is fired for it. So you can get the sender DataGridView this way: var grid = (DataGridView)sender;. – Reza Aghaei"
so i just replaced this line :
DataGridView datagridcell = new DataGridView();
by this one:
var grid = (DataGridView)sender;
so i can use "grid" variable to manipulate my current datagridview
final code looks like that :
private void dataGridView13_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
var grid = (DataGridView)sender; //get current datagridview
string codearticle = "'" + CodeArticle0.Text + "'";
string division = "'" + Division0.Text + "'";
string colonne = grid.Columns[e.ColumnIndex].HeaderText; //get column headertext of current cell
string valeur = grid[e.ColumnIndex, e.RowIndex].Value.ToString(); //get new value of current cell
string Ligne = grid[3, 0].Value.ToString();
Controller.ControllerMenuPrincipal.updateQALigneFab(codearticle, division, colonne, valeur, Ligne); //calling stored procedure to update my table
}
Upvotes: 1