the first cell in my table is automatic blue c# - c#

When i populate my datagridview the first cell in the datagridview is always blue. The data grid view is populated with DataGridViewTextBoxColumn. it's like I have pressed the cell but I have not.it is like that with every table I create. i dont want it to be blue, can somebody help?

By default, there's always a selection on a DataGridView. If you don't want it, you can do:
// hides the "blue"
theDataGridView.ClearSelection();
// hides the "focus rectangle"
theDataGridView.CurrentCell = null;
As soon as the control gets focused again, you'll have a focus rect though.

This is because by default the selection mode will be singlecell you can change that. Also in code behind you can set the dgGridView.SelectedIndex = -1
or clearselection like that
Remove blue colored row from DataGridView WinForms

dataGridView.ClearSelection();
and then set the current cell property to null
dataGridView.CurrentCell = null; // for no focus

Related

How To Disable a Button In Datagridview C# [duplicate]

I have a datagridview on a form with some data. The 1st column contains button for deleting the row. How can we disable this button or the entire row based on some condition, so the row cannot be deleted?
Would you consider just turning the button cell into a regular empty text box disabled?
Dim cell As DataGridViewButtonCell = dgv.row(x).cell(y)
cell = New DataGridViewTextBoxCell()
cell.value = String.Empty
cell.ReadOnly = True
It loses its bordered "Button" appearance and blends in with the remainder of the cells (assuming you are using primarily the default DataGridViewTextBoxCells).
Here's the equivalent in C#, plus it grays out the field to make it look read-only:
var cell = dgv[column, row] = new DataGridViewTextBoxCell();
cell.Value = ""; // ignored if this column is databound
cell.ReadOnly = true;
cell.Style.BackColor = Color.FromKnownColor(KnownColor.Control);
There's actually a HowTo on MSDN doing exactly this.
Edit: Added some other suggestions.
You can make the button column invisible.
Or if you only want to disable deletion of certain rows you could put in true or false in each DataGridViewRows Tag property and in your button event handler you only delete the ones that are set to false. You could possibly combine this with just changing the foreground and background colours of the cell to make it look disabled, this colouring in could probably be done in the CellFormatting event handler or something so that you don't have to loop through and colour it in by hand.
This is an old post, but I would like to suggest what I do.
If conditionToDisable Then
Dim cell As New DataGridViewTextBoxCell 'Replace the ButtonCell for a TextCell'
cell.Value = valueForCell 'Set the value again'
grid.Rows(r).Cells(c) = cell 'Override the cell'
End If
I hope this is helpful.

How to focus cell in row for adding new record in DataGridView?

How can I programmatically focus cell in WinForms DataGridView shown highlighted on sample below?
I can easily set focus to any cell in rows 0~3 but that cell is in row 4 which looks like "virtual" because dataGridView1.Row(4) does not exist.
Not sure if i have quite got your question but does DataGridView1.NewRowIndex not work for what you want?
I tried DataGridView1.Item(0, DataGridView1.NewRowIndex).Selected = True which seems to do what I think you are trying to achieve.
The code above is on the click event of button 1 and produces the following:
If you want to 'un-select' any currently selected cell first then precede the above with If Not IsNothing(DataGridView1.CurrentCell) Then DataGridView1.CurrentCell.Selected = False, the If Not IsNothing... is to cover the code running where no cell is currently selected.
BTW I don't think you can use .CurrentCell for a cell selected on the new row, but you can use .item

How to make a row select, highlight specific cells

How to make a Row select highlight specific Cells and not all the Row in a Grid View. I have explored all the questions on SOF, but there is nothing is available.
Set the SelectionMode property of the Grid to CellSelect
You need the set the selection mode on the Grid to cell selection.
If you want it to act like a row select but keep a few cells un-highlighted you should use CellSelect as the selection mode. Then register on row selection changed and set the cell.Selected = true; for the ones you want selected, and false for the rest.
I found the solution !! If you want to control the highlight of a row to a certain range of cells, then you need to control the behavior of the cells in question and make them behave differently when the a row is selected.
YourGridviewInstance.GetCellRange(int topRow,int leftColumn,int buttomRow,inr RightColumn)
in other words
YourGridviewInstance.GetCellRange(YourCurrentRow, The column you want to affect,YourCurrentRow, The Same Column you want to affect)

Rows in DataGridView are not changing color

I am just trying to put the color red for the back color of the rows that have the value of 4 in a specific column but all the rows have white back color even though there are rows with value more than 4. I also stepped through the code so I know that the code actually execute the code to change the backcolor.
What am I doing wrong here.
dataGridViewMain.DataSource = table;
dataGridViewMain.Sort(dataGridViewMain.Columns["Days in the shop"], ListSortDirection.Descending);
foreach (DataGridViewRow row in this.dataGridViewMain.Rows)
{
if (Convert.ToInt32(row.Cells["Days in the shop"].Value) > 4)
row.DefaultCellStyle.BackColor = Color.Red;
}
Apparently I need to put the logic inside RowPostPaint event. It's all good now.
I have had problems with DataGridView cell colors when creating child forms, it was hard for me to find a solution. If using a child form I had to make sure I changed the color from the Form Load Event. I was initially trying to make changes to color from the main method, which did not work.

How to focus on a specific cell when adding a new row to Datagrid?

When using Silverlight/WPF Datagrid and you add a new row to the existing collection, how can I jump into a specific cell's edit mode in order to hint to the user that this field needs to be filled out right away?
Many Thanks,
This is how I was able to make it work in SL 5 RC.
dg.ItemsSource.Add(data);
dg.SelectedItem = data; //set SelectedItem to the new object
dg.ScrollIntoView(data, dg.Columns[0]); //scroll row into view, for long lists, setting it to start with the first column
dg.Focus(); //required in my case because contextmenu click was not setting focus back to datagrid
dg.BeginEdit(); //this starts the edit, this works because we set SelectedItem above
Hope this helps.
In Silverlight 4 it is:
dg.SelectedItem = data;
dg.CurrentColumn = dg.Columns[1]; // You have to use this line instead
dg.Focus();
dg.BeginEdit();

Categories

Resources