I want my DataGridView to highlight row headers for any rows that have selected cells. Any way to do that?
(Standard behavior seems to be only to highlight the row header if the entire row of cells is selected).
What I've tried:
I've looked at the decompiled source for DataGridView and from what i can tell (which definitely may be wrong) it highlights the header based upon whether the DataGridViewRow is selected. I can't figure out how to set the row selected without the whole row actually being selected
The way I've done this before is to handle the CellStateChanged event, check the cells in the effected cell's row, and set the row header accordingly.
private void DataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
bool selected = false;
foreach (DataGridViewCell cell in e.Cell.OwningRow.Cells)
{
if (cell.Selected)
{
selected = true;
break;
}
}
e.Cell.OwningRow.HeaderCell.Style.BackColor = selected ?
this.dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor :
this.dataGridView1.RowHeadersDefaultCellStyle.BackColor;
}
You'll need to set the following snippit to enable these changes, as explained here:
this.dataGridView1.EnableHeadersVisualStyles = false;
Not shown: also consider coloring the header cell for Row[0] when everything is initialized.
Related
I am working with a data grid for Visual Studio forms. When I select a given cell, I want to keep track of the index of the row that cell is on, using the following code:
selectedRows.Clear();
for (int i = 0; i < dataGrid.RowCount; i++)
{
if (dataGrid[column, i].Selected)
{
selectedRows.Add(i);
}
}
This works 80% of the time. However, occasionally, when I select the name cell of a row, it highlights the cell blue, which leads me to believe I selected it, but the index is not added to selectedRows. Is there something I am missing here?
Am I correct in assuming that sometimes the cells Selected property is simply not set to true, which is causing this error, even though I have clearly selected the cell?
P.S. I have checked that the "column" variable is correct.
You can use the Cell click event and do something like this:
private void Datagridview1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int index = e.RowIndex;
if (!selectedRows.Exists(x =>x == index))
{
selectedRows.Add(index)
}
}
Greeting to all,
Please help me how to set back again the tooltip text of row header of datagridview, When I load the datatable and set each tooltip for rows of datagridview, its show correctly when I move mouse pointer to row header but when I click the column to sort the data to ascending or descending the tooltip for the row header was remove ? how I can set it back or to avoid it when I clicking the column header of datagridview .... thanks in advance !
Set ToolTipText property on DataGridViewColumn
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.ToolTipText = "Tooltip"; // set here.
}
Move the code that sets your row tooltips to the DataBindingComplete event handler. This handle will trigger every time the DataSource updates (which includes sorting). Like so:
this.dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete;
private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
row.HeaderCell.ToolTipText = "ToolTip Text";
}
}
In telerik Rad GridView (2014) Winform, i have a gridview with a disabled column but if i type an especific text in another column i want that column to be enabled to write, but only the cell for that specific row
Since you want to some cells editable, you should not disable the whole column. For the purpose of your case, you can use the CellBeginEdit event, which you can cancel if your condition is not met, hence the user will not be able to edit the specified cell:
void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
if (e.Column.Name == "columnAtHand" && e.Row.Cells["DependantColumn"].Value == "disallowEdit")
{
e.Cancel = true;
}
}
I have a simple RadGridView, inside the first cell i have Checkbox in which I want to change the color of my row if this Checkbox is checked state and return to the original color if this Checkbox is in unchecked state.
Currently this is what i have try and this paint my Row when my Checkbox is checked but how can i change to the original color when my Checkbox is uncheckd ?
private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
if ((bool)e.RowElement.RowInfo.Cells["Select"].Value == true)
{
e.RowElement.DrawFill = true;
e.RowElement.GradientStyle = GradientStyles.Solid;
e.RowElement.BackColor = Color.SkyBlue;
}
else
{
e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
}
}
The code you have is correct and it should change the color of the rows with checked first cell. However, there is one thing to note. The editor of the check box cell is permanent editor, and its value will be submitted to the cell, only after you change the current row in the grid. If this does not work for you, there is an easy way to submit the value immediately upon changing the check box state. Here you can find out how to do it: http://www.telerik.com/help/winforms/gridview-editors-howto-checkboxeditor-submit-value-change.html
I'm looking for a way to enable multi row select in a DataGridView-Control but disable multi cell select.
What I've tried so far:
DataGridView.MultiSelect = true allows to select multi rows and cells
ClearSelection() in DataGridView_CellMouseClick-Event and re-select the last selected cell doesn't look very nice (you see the old cell deselect and then the new cell select; SuspendLayout() and ResumeLayout() doesn't help)
DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect is not an option: If the user clicks a cell, it should only this cell get selected
It's for an export function: The user should be able to export selected rows to a file but in general he should not be able to select more than one cell (for copy & paste etc.).
Regards,
inno
----- [UPDATE] -----
Here's my implementation. Works fine (comments removed for compactness):
using System.Windows.Forms;
namespace YourAmazingNamespace
{
public partial class SpecialSelectDataGridView: DataGridView
{
public SpecialSelectDataGridView()
{
InitializeComponent();
}
protected override void SetSelectedCellCore(int columnIndex, int rowIndex, bool selected)
{
ResetSelectedCells();
base.SetSelectedCellCore(columnIndex, rowIndex, selected);
}
void ResetSelectedCells()
{
foreach (DataGridViewCell cell in SelectedCells) {
base.SetSelectedCellCore(cell.ColumnIndex, cell.RowIndex, false);
}
}
}
}
Multiple rows are selected through MultiSelect = true (default value) and currently selected cells are resetted by calling ResetSelectedCells() before selecting the new one.
HTH, thanks and regards,
inno
You could override SetSelectedRowCore or SetSelectedCelCore and perform your custom selection.
MSDN Quote:
The DataGridView control uses this
method whenever it changes the
selection state of a cell. The
selection state changes without regard
to the current SelectionMode property
value, and without changing the
CurrentCell property value. This is
useful when you want to implement your
own selection modes
Of course this means you will have to use an derived datagrid and not the standard one.