Right click to select a row in a Datagridview and show a menu to delete it

I finally solved it: In Visual Studio, create a ContextMenuStrip with an item called “DeleteRow” Then at the DataGridView link the ContextMenuStrip Using the code below helped me getting it work. this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown); this.DeleteRow.Click += new System.EventHandler(this.DeleteRow_Click); Here is the cool part private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Right) { … Read more

How to change the color of winform DataGridview header?

The way to do this is to set the EnableHeadersVisualStyles flag for the data grid view to False, and set the background colour via the ColumnHeadersDefaultCellStyle.BackColor property. For example, to set the background colour to blue, use the following (or set in the designer if you prefer): _dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue; _dataGridView.EnableHeadersVisualStyles = false; If you … Read more

How to disable the ability to select in a DataGridView?

I’d go with this: private void myDataGridView_SelectionChanged(Object sender, EventArgs e) { dgvSomeDataGridView.ClearSelection(); } I don’t agree with the broad assertion that no DataGridView should be unselectable. Some UIs are built for tools or touchsreens, and allowing a selection misleads the user to think that selecting will actually get them somewhere. Setting ReadOnly = true on … Read more

Find a row in dataGridView based on column and value

This will give you the gridview row index for the value: String searchValue = “somestring”; int rowIndex = -1; foreach(DataGridViewRow row in DataGridView1.Rows) { if(row.Cells[1].Value.ToString().Equals(searchValue)) { rowIndex = row.Index; break; } } Or a LINQ query int rowIndex = -1; DataGridViewRow row = dgv.Rows .Cast<DataGridViewRow>() .Where(r => r.Cells[“SystemId”].Value.ToString().Equals(searchValue)) .First(); rowIndex = row.Index; then you can … Read more

How to enable DataGridView sorting when user clicks on the column header?

Set all the column’s (which can be sortable by users) SortMode property to Automatic dataGridView1.DataSource = students.Select(s => new { ID = s.StudentId, RUDE = s.RUDE, Nombre = s.Name, Apellidos = s.LastNameFather + ” ” + s.LastNameMother, Nacido = s.DateOfBirth }) .OrderBy(s => s.Apellidos) .ToList(); foreach(DataGridViewColumn column in dataGridView1.Columns) { column.SortMode = DataGridViewColumnSortMode.Automatic; } Edit: … Read more

Horrible redraw performance of the DataGridView on one of my two screens

You just need to make a custom class based off of DataGridView so you can enable its DoubleBuffering. That’s it! class CustomDataGridView: DataGridView { public CustomDataGridView() { DoubleBuffered = true; } } As long as all of my instances of the grid are using this custom version, all is well. If I ever run into … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)