Index of Currently Selected Row in DataGridView
There is the RowIndex property for the CurrentCell property for the DataGridView. datagridview.CurrentCell.RowIndex Handle the SelectionChanged event and find the index of the selected row as above.
There is the RowIndex property for the CurrentCell property for the DataGridView. datagridview.CurrentCell.RowIndex Handle the SelectionChanged event and find the index of the selected row as above.
To handle the DatGridViews CheckedChanged event you must first get the CellContentClick to fire (which does not have the CheckBoxes current state!) then call CommitEdit. This will in turn fire the CellValueChanged event which you can use to do your work. This is an oversight by Microsoft. Do some thing like the following… private void … Read more
This trick works for me: grd.DataSource = DT; // Set your desired AutoSize Mode: grd.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; grd.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; grd.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; // Now that DataGridView has calculated it’s Widths; we can now store each column Width values. for (int i = 0; i <= grd.Columns.Count – 1; i++) { // Store Auto Sized … Read more
You can use the CellMouseEnter and CellMouseLeave to track the row number that the mouse is currently hovering over. Then use a ContextMenu object to display you popup menu, customised for the current row. Here’s a quick and dirty example of what I mean… private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) … Read more
Not tested, but I think you can do the following: dataGrid.Rows[index].Selected = true; or you could do the following (but again: not tested): dataGrid.SelectedRows.Clear(); foreach(DataGridViewRow row in dataGrid.Rows) { if(YOUR CONDITION) row.Selected = true; }
You’ve added a button to your DataGridView and you want to run some code when it’s clicked. Easy peasy – just follow these steps: Don’ts First, here’s what NOT to do: I would avoid the suggestions in some of the other answers here and even provided by the documentation at MSDN to hardcode the column … Read more
You need to loop through the rows in the datagridview and then compare values of columns 7 and 10 on each row. Try this: foreach (DataGridViewRow row in vendorsDataGridView.Rows) if (Convert.ToInt32(row.Cells[7].Value) < Convert.ToInt32(row.Cells[10].Value)) { row.DefaultCellStyle.BackColor = Color.Red; }
You can do: DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells[0].Value = “XYZ”; row.Cells[1].Value = 50.2; yourDataGridView.Rows.Add(row); or: DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells[“Column2”].Value = “XYZ”; row.Cells[“Column6”].Value = 50.2; yourDataGridView.Rows.Add(row); Another way: this.dataGridView1.Rows.Add(“five”, “six”, “seven”,”eight”); this.dataGridView1.Rows.Insert(0, “one”, “two”, “three”, “four”); From: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx