I want to programmatically generate a click on a DataGridView row in C #

I have a DataGridView in the form, and I want to programmatically click its first row. I found code to select its rows or columns from code.

For example,

 datagridview.Columns[0].Selected = true; datagridview.Rows[0].Selected = true; 

However, this code does not raise click event on datagridview. If someone has encoded how to click a datagridview from code, please extend your kind help.

+6
c # datagridview
source share
5 answers

Just call the event handler method, for example:

 datagridviewRowClickedEventHandler(new object(), new eventargs()); 

If you use the sender or e parameters in the event handler, you will need to figure out how to pass the correct values.

+11
source share

Paste the appropriate code into your project (usually in a form that has a datagridview).
Be sure to change the name of the DataGridView from dataGridView1 to the appropriate one in your form.

 private void Form1_Load(object sender, EventArgs e) { //call the cell click event with the first cell as the parameters. dataGridView1_CellClick(dataGridView1, new DataGridViewCellEventArgs(0, 0)); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { //put your code for handling cell click here } 
+7
source share

It looks like you have the first half by setting the propers Selected to true. Now you can program the call to the string handler, and it should act as if you clicked it in the GUI.

+2
source share
 datagridview.Columns[0].Selected = true; datagridview.Rows[0].Selected = true; 

It displays the row as selected, but it will not change dataGridView.CurrentRow . So this can be a problem.

 dataGridView.CurrentCell = dataGridView[<column>, <row>]; 

will also change the value of CurrentRow .

Hope this helps.

0
source share

I assume you want to apply a DataSource and select the first row? Correctly?

Best way to do it like this

 private async void DgvAreas_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e) { } 

And here is the code to simulate a click on a line.

 DgvAreas_RowStateChanged(dgvAreas, new DataGridViewRowStateChangedEventArgs(dgvAreas.Rows[0], DataGridViewElementStates.Selected)); 

In my case, I have 3 DataGridView , so I easily populate the first one. The second one I populate when the user clicks the first DataGridView, in which case I use the DgvStaff_RowStateChanged event.

And in this case, DgvStaff_RowStateChanged , I have code to get async data and fill out the third DataGridView , and after applying the data source for the second DataGridView I need to get the data for the first row of this view and display it in the third DataGridView . This is cascading logic .

 private async void DgvStaff_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e) { try { // For any other operation except, StateChanged, do nothing if (e.StateChanged != DataGridViewElementStates.Selected) return; if (sender is MetroFramework.Controls.MetroGrid) { if ((sender as MetroFramework.Controls.MetroGrid).SelectedRows.Count > 0) { dgvGeoData.DataSource = null; dgvAreas.DataSource = null; metroProgressSpinnerMain.Visible = true; panelFilter.Enabled = false; dgvAreas.RowStateChanged -= DgvAreas_RowStateChanged; var selectedRow = (sender as MetroFramework.Controls.MetroGrid).SelectedRows[0]; var machineModelShortView = (MachineModelShortView)selectedRow.DataBoundItem; var startTime = Convert.ToDateTime(dateTimePickerStart.Value.ToShortDateString()); var endTime = Convert.ToDateTime(metroDateTimeEnd.Value.ToShortDateString()); var areas = await UpdateAreaItems(machineModelShortView.MachineID, startTime, endTime); if (areas.Any()) { BeginInvoke((Action)(() => { dgvAreas.DataSource = areas.OrderBy(x => x.AreaID).ThenBy(x => x.TimeStart).ToList(); dgvAreas.RowStateChanged += DgvAreas_RowStateChanged; // !!! This is how you simulate click to the FIRST ROW dgvAreas.Rows[0] DgvAreas_RowStateChanged(dgvAreas, new DataGridViewRowStateChangedEventArgs(dgvAreas.Rows[0], DataGridViewElementStates.Selected)); metroProgressSpinnerMain.Visible = false; panelFilter.Enabled = true; })); } else { BeginInvoke((Action)(() => { metroProgressSpinnerMain.Visible = false; panelFilter.Enabled = true; })); } } } } catch (Exception ex) { logger.Error(ex); } } 

And here

  private async void DgvAreas_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e) { try { // For any other operation except, StateChanged, do nothing if (e.StateChanged != DataGridViewElementStates.Selected) return; //Get GeoData if (sender is MetroFramework.Controls.MetroGrid) { if ((sender as MetroFramework.Controls.MetroGrid).SelectedRows.Count > 0) { dgvGeoData.DataSource = null; metroProgressSpinnerMain.Visible = true; panelFilter.Enabled = false; var selectedRow = (sender as MetroFramework.Controls.MetroGrid).SelectedRows[0]; var areaItem = (AreaItem)selectedRow.DataBoundItem; var geoData = await UpdateWDataPositionItems(areaItem.MachineID, areaItem.TimeStart, areaItem.TimeEnd.Value); if (geoData.Any()) { BeginInvoke((Action)(() => { dgvGeoData.DataSource = geoData.OrderBy(x => x.AtTime).ToList(); metroProgressSpinnerMain.Visible = false; panelFilter.Enabled = true; })); } else { BeginInvoke((Action)(() => { metroProgressSpinnerMain.Visible = false; panelFilter.Enabled = true; })); } } } } catch (Exception ex) { logger.Error(ex); } } 
0
source share

All Articles