Reputation: 11
Apologies in advance if this question has been answered elsewhere (haven't found the answer after a few hours of searching).
Anyways, my code works, I've tested it using a button. Selecting the row then clicking the button and it displays the correct data.
But I want it to display the data when I select row , instead of selecting a row then pressing a button.
My code:
private void dgvRecipes_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
if (dgvRecipes.SelectedRows.Count > 0)
{
int selectedRecipe =
int.Parse(dgvRecipes.SelectedRows[0].Cells[0].Value.ToString());
lblRecipe.Text = dgvRecipes.SelectedRows[0].Cells[1].Value.ToString();
string selectStr = "SELECT IngredDesc, IngredAmt, IngredComment " +
"FROM Ingredient, RecipeIngredients
WHERE RecipeIngredients.RecipeNo = " + selectedRecipe +
" AND Ingredient.IngredID = RecipeIngredients.IngredID ";
dbCmd = new SqlCommand(selectStr, dbConn);
dbAdapter = new SqlDataAdapter(dbCmd);
dbAdapter.Fill(ds, "RecipeIngredients");
dgvIngredients.DataSource = ds.Tables["RecipeIngredients"];
selectStr = "SELECT InstructionNo, Instructions FROM RecipeInstructions "
+ "WHERE RecipeInstructions.RecipeNo = " + selectedRecipe;
dbCmd = new SqlCommand(selectStr, dbConn);
dbAdapter = new SqlDataAdapter(dbCmd);
dbAdapter.Fill(ds, "RecipeInstructions");
dgvInstructions.DataSource = ds.Tables["RecipeInstructions"];
}
}
Again, apologies if I haven't phrased my question correctly and set out the code badly.
Upvotes: 0
Views: 64
Reputation: 14389
You just need to add a Selection Change event of Datagridview:
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
......
}
To explain TaW
usefull comment you will have to go in DatagridView's properties
(from VS desgin) select the Events
tab and Double click on the SelectionChange
Event
Upvotes: 1
Reputation:
Use SelectionChanged event of DataGridView
.
SelectionChanged event occurs whenever cells are selected or the selection is canceled, whether programmatically or by user action.
Upvotes: 0
Reputation:
You normally do this by a binding, so the textboxes display the same values without any extra code behind. But: you can also change the property called "selectionmode" of a datagrid; you should set in to selectRow. Then you can find the appropriate event and move the code you have under your button to that event
Upvotes: 0