DataGridViewRichTextBox row height issue - c#

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.

Related

How to hide columns in Datagridview winform application?

How can I hide a column in datagridview like this:
please click here
I searched for it in datagridview properties but found nothing.
If you would jot down your requirement, I think you'd know the answer:
Requirement If the width of a column becomes 0 (or very small?), then the column should become invisible
const int minimumColumnWidth = 2; // can this be 0?
this.DataGridView1.ColumnWidthChanged += OnColumnWidthChanged;
private void OnColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
DataGridViewColumn column = e.Column;
column.Visible = column.Width > minimumColumnWidth;
}
Of course you have to invent a method to make the column visible again.

Show Datagridview Rowheader Tooltip

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

Select Dropdown item in datagridview column

I have to manually select the item from the dropdownlist that is in the datagridview column, but the issue is while selecting the item i need to click on the dropdownlist multiple times.
How to resolve this? Any help will be highly appreciated.
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Data";
cmb.Name = "cmb";
dgv2.Columns.Add(cmb);
Set EditMode property of the DataGridView to EditOnEnter: link
DataGridView.EditMode - Gets or sets a value indicating how to begin editing a cell.
EditOnEnter - Editing begins when the cell receives focus.
The below code must be tied into the CellClick event of the datagridview:
private void datagridview_CellClick(object sender, DataGridViewCellEventArgs e)
{
bool validRow = (e.RowIndex != -1); //Make sure the clicked row isn't the header.
var datagridview = sender as DataGridView;
// Check to make sure the cell clicked is the cell containing the combobox
if(datagridview.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && validRow)
{
datagridview.BeginEdit(true);
((ComboBox)datagridview.EditingControl).DroppedDown = true;
}
}
Try Setting EditMode property to EditOnEnter.
I hope this helps!

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

Categories

Resources