Show Datagridview Rowheader Tooltip - c#

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";
}
}

Related

highlight row header for rows with any selected cells

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.

Select the wpf datagrid cell at runtime

My scenario:
Data loads in to my data grid page by page
On key down of the last row on w pf data grid, i will load another row in the end. this way after binding with the new rows after key down focus on the last selected cell is lost. i want to retain the selection on the last+1 (row and column) cell. How can i set this.
There is event SourceUpdated, so you could do something like that:
private void DataGrid_SourceUpdated(object sender, DataTransferEventArgs e)
{
(sender as DataGrid).SelectedIndex = (sender as DataGrid).Items.Count - 1;
}
It could help also:
How to select a row or a cell in WPF DataGrid programmatically?

Right align a column in datagridview doesn't work

I am having a datagridiview which is dynamically bound to a datatable. I would like to align some of the columns in header to right aligned.
I tried this setting for the datagridview for both cellstyle and headercell. For cell style it is showing correctly but for header it is not:
The code I used:
this.dataGridView1.Columns["Quantity"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
Can some one help me?
The code works: the space you see at the right of the header text is "normal".
The DataGridView supports sorting by columns. Therefore, each column header reserves enough space to display the sort glyph (usually an arrow).
If you want the text in column header to be perfectly right aligned, you'll need to disable sorting. Set the SortMode property for the column to NotSortable. This will prevent space from being reserved for the sort glyph.
object lesson:
public class FrmTest : Form
{
public FrmTest()
{
InitializeComponent();
this.DataGridView1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
this.DataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
}
private void CheckBox1_CheckedChanged(System.Object sender, System.EventArgs e)
{
if (this.CheckBox1.Checked) {
this.DataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.Automatic;
} else {
this.DataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
}
this.DataGridView1.Refresh();
}
}
1/ After loading the form:
2/ Allow sorting by clicking the checkbox:
3/ After clicking the column:
For set the the align in column header or in cell content you can use the IDE and open this property mask of dataGridView.
Set the align cell content in Colunm property or set the row header aling in RowHeaderDefaultCellStyle
foreach (DataGridViewColumn col in dataGridView2.Columns){
col.SortMode = DataGridViewColumnSortMode.NotSortable; // This first set it work
col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}

DataGridViewRichTextBox row height issue

I am adding a RichTextBox column in my DataGridView.
In the DataGridView the RichTextBox height of the row is set automatically.
and the text does not appear properly.
So how can I set the row Height?
I also tried
datagridview1 row1=new datagridview();
row1.height=100;
but the row height is not set properly.
Please give me suggestions.
you can change it with the CellPainting event
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex != -1)
{
dataGridView1.Rows[e.RowIndex].Height = 100;
}
}
hi again nitesh,about your request i think this is what you want...
dataGridView1.Rows[e.RowIndex].Height = dataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Displayed).Width;
or...since we assume at least 1 column exists...
dataGridView1.Rows[e.RowIndex].Height = dataGridView1.Columns[0].Width;
If you wish to set it at design time:
In the properties for the DataGridView, find the RowTemplate property;
click the arrow on the right of RowTemplate, reveailng more details;
edit the Height property.

Changing DataGridViewButtonColumn's Button Text Per Row

I have a DataGridView with one column of type DataGridViewButtonColumn.
I want to put a text on the button. I've tried in the Edit Column options to put the text in the Text properties. I have even tried swtteing UseColumnTextForButtonValue = true.
I can't find a way to make it work.
Did you want to bind DataGridViewButtonColumn Text to the DataGridView's DataSource? If so then set the DataPropertyName for the column via the Edit Column options.
Otherwise you can try using the DataGridView's CellFormatting event:
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Value = "some value";
}
}
Assuming that your DataGridView name is DataGridView1
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCell cell = row.Cells[0]; //Column Index for the dataGridViewButtonColumn
cell.Value = "Your Text";
}

Categories

Resources