Changing datagridview cell color based on condition

I may suggest NOT looping over each rows EACH time CellFormating is called, because it is called everytime A SINGLE ROW need to be refreshed. Private Sub dgv_DisplayData_Vertical_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgv_DisplayData_Vertical.CellFormatting Try If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells(“LevelID”).Value.ToString() = “6” Then e.CellStyle.BackColor = Color.DimGray End If If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells(“LevelID”).Value.ToString() = “5” Then e.CellStyle.BackColor = Color.DarkSlateGray End … Read more

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 use textbox to search data in data grid view?

The likely reason you are seeing a blank DataGridView is due to your filter string searching for exact matches to the TextBox text. “Name=”{0}”” Because you are updating this filter in the TextBox.TextChanged event, the first time you enter a character – no matches are found. For example, given the following grid: ╔════╦══════╗ ╔════════╗ ║ … Read more

Search for value in DataGridView in a column

Why you are using row.Cells[row.Index]. You need to specify index of column you want to search (Problem #2). For example, you need to change row.Cells[row.Index] to row.Cells[2] where 2 is index of your column: private void btnSearch_Click(object sender, EventArgs e) { string searchValue = textBox1.Text; dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect; try { foreach (DataGridViewRow row in dgvProjects.Rows) … Read more

How to improve painting performance of DataGridView?

I recently had some slowness issues with DataGridView and the solution was the following code public static void DoubleBuffered(this DataGridView dgv, bool setting) { Type dgvType = dgv.GetType(); PropertyInfo pi = dgvType.GetProperty(“DoubleBuffered”, BindingFlags.Instance | BindingFlags.NonPublic); pi.SetValue(dgv, setting, null); } It turns double buffering on for DataGridView objects. Just call DoubleBuffered() on your DGV. Hope it … Read more

Check/Uncheck a checkbox on datagridview

Looking at this MSDN Forum Posting it suggests comparing the Cell’s value with Cell.TrueValue. So going by its example your code should looks something like this:(this is completely untested) Edit: it seems that the Default for Cell.TrueValue for an Unbound DataGridViewCheckBox is null you will need to set it in the Column definition. private void … Read more

Adding Text to DataGridView Row Header

private void dtgworkingdays_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { this.FillRecordNo(); } private void FillRecordNo() { for (int i = 0; i < this.dtworkingdays.Rows.Count; i++) { this.dtgworkingdays.Rows[i].HeaderCell.Value = (i + 1).ToString(); } } Also see Show row number in row header of a DataGridView.

DataGridViewComboBoxCell Binding – “value is not valid”

I managed to find the solution not long after posting the question. For anyone else: The problem was that I was trying to assign the DataGridViewComboBoxCell.Value to an object, expecting that because the Cell was bound to a data source that it would automatically find the object in the source and update. This wasn’t actually … Read more

How do I change the datagridview selected row background color?

Come on man… there has to be a simple solution, and finally got one. dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Blue; dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red; This worked for me, no complex codes, no event handling. I did it before but was not able to recall so thought posting it would help others and me in future 🙂