add new line in gridview cell - c#

I have a gridview that display grade, but i don't want to display in one line. What i have tried is like this picture it became wrap text.
the code
gridView_Attendees.Columns["grade"].Caption = "Grade";
gridView_Attendees.Columns["grade"].Width = 150;
gridView_Attendees.Columns["grade"].OptionsFilter.AutoFilterCondition = AutoFilterCondition.Contains;
gridView_Attendees.OptionsView.RowAutoHeight = true;
gridView_Attendees.Columns["grade"].AppearanceCell.TextOptions.WordWrap = WordWrap.Wrap;
RepositoryItemMemoEdit memoEdit = new RepositoryItemMemoEdit();
memoEdit.ReadOnly = true;
memoEdit.AutoHeight = true;
memoEdit.WordWrap = true;
gridView_Attendees.GridControl.RepositoryItems.Add(memoEdit);
gridView_Attendees.Columns["grade"].ColumnEdit = memoEdit;
what i want is add new line to break into 2 lines, precisely before text Technique. like this pic

To achieve your goal, assign MemoEdit editor as a cell in-place editor
and set the View's OptionsView.RowAutoHeight property to True.
You have already used this approach but it does not work as you want so you can emulate the desired behavior by handling the GridView.CustomDrawCell event: draw the multi-line text and set the e.Handled property to true to prevent the default painting of the grid's cell's content. In this case, you need to increase the grid's row height. To do it, set the GridView.RowHeight property or handle the GridView.CalcRowHeight event , to set the individual height for the every grid's row (in this case, the GridView.OptionsView.RowAutoHeight property should be set to false).
References:
Display multi-line text in gridview cell
Multi line text indentation in XtraGrid memoedit cell
gridview cell display multiline text

Related

Horizontal scrollbar not showing on my textbox

On a Winform C# application, i display a textbox on my form.
This textbox will display one line, just one.
I would like to show and be abe to use an horizontal scrollbar.
I set the property "scrollbar" to horizontal : ScrollBar doesn't show.
I add WordWrap to false : ScrollBar doesn't show.
I add MultiLine to true ( even if one ligne ) : ScrollBar doesn't show.
My line displayed is a loter "longer" than the contrĂ´le, so i really need a scrollbar :(
Here is the definition :
this.TxtBox_ApercuFichier.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.TxtBox_ApercuFichier.Location = new System.Drawing.Point(11, 30);
this.TxtBox_ApercuFichier.Multiline = true;
this.TxtBox_ApercuFichier.Name = "TxtBox_ApercuFichier";
this.TxtBox_ApercuFichier.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
this.TxtBox_ApercuFichier.Size = new System.Drawing.Size(702, 21);
this.TxtBox_ApercuFichier.TabIndex = 12;
Even with wordwrap at false, it's the same result.
( My textbox is in a groupbox).
Any idea please ?
Thanks a lot :)
Regards,
You need to do the following to get a horizontal scroll bar to display in a windows forms text box:
this.TxtBox_ApercuFichier.Multiline = true;
this.TxtBox_ApercuFichier.WordWrap = false;
this.TxtBox_ApercuFichier.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
You can then resize the text box to give the appearance of one line. You need to have Multiline enabled otherwise the height of the text box will be set to the text height (I can't seem to find an easy way to override this), hence you not being able to see the scroll bar.
The following code will set ScrollBar not visible and also the parent panel where its contained.
HScrollBar hScroller = textBox.HScrollBar;
hScroller.Visible = false;
hScroller.Parent.Visible=false;

Can a C# DataGridView cell be both Multiline and single line?

I'm building a DataGridView dynamically, and the textboxes potentially have an AutoComplete custom source associated with them. Under some circumstances, when I set textbox.Multiline to true, I get the following effect. If I'm not actively editing the cell, it looks like this:
However, if I try to edit the cell, it looks like this:
If I move the cursor around this edit mode cell, focus hops out of the cell entirely, instead of moving the text or expanding the cell height. It looks as if it's trying to be in multiline and single line mode at the same time, but I'm not sure.
Any thoughts?
I found a solution - not a root cause. In EditingControlShowing, I do the following:
textBox.Dock = textBox.Multiline ? DockStyle.Fill : DockStyle.None;
where I've set textBox = (TextBox) e.Control;
It's a sledgehammer solution, but it works.
Lets give a name to the DataGridView object: dgv,
So On dgv_CellEnter event add following lines
int idxcol = dgv.Columns["YourColumnName"].Index;
if (e.ColumnIndex == idxcol)
dgv.Columns[idxcol].DefaultCellStyle.WrapMode = DataGridViewTriState.False;
and on dgv_CellLeave event add the following lines
int idxcol = dgv.Columns["YourColumnName"].Index;
if (e.ColumnIndex == idxcol )
{
dgv.Columns[e.ColumnIndex].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
}

Getting "No room is available to display rows" when setting gridView.FirstDisplayedScrollingRowIndex

gridView.FirstDisplayedScrollingRowIndex = gridView.SelectedRows[0].Index;
blowing up with:
No room is available to display rows
DataGridView is customized control in the unbound mode. datagridview.FirstDisplayedScrollingRowIndex is not set in a specific datagridview based event.
Rows are added to the datagridview via gatagridview.Rows.Add method on DataSet.EndMerge then cells are styled and formatted row by row. Last selected row is cleared and restored and scroll bar position restored to the first visible row.
Trying find out what this exception actually means.
if (gridView.Rows.Count > 0)
{
gridView.ClearSelection();
T value = GetItemByRow(gridView.Rows[0]);
bool isVisible = filter.ShouldShow(value);
gridView.Rows[0].Selected = true;
if (!isVisible)
{
gridView.Rows[0].Visible = true;
gridView.FirstDisplayedScrollingRowIndex = gridView.SelectedRows[0].Index;
gridView.Rows[0].Visible = false;
}
else
{
gridView.FirstDisplayedScrollingRowIndex = gridView.SelectedRows[0].Index;
}
}
When the grid is automatically sized to be 0 height (or probably width) and you set
dataGridView.FirstDisplayedScrollingRowIndex = 0;
the exception will be thrown. Our grid has set Dock = Fill and in some cases was sized to be invisible.
Our fix was to set a MinimumSize to 100/50, and we never saw the exception again. Even if the grid is not within the window (and thus invisible) the exception will now not be thrown.
When are row is present in the grid but not visible setting it to be first row will make it throw this exception.

Hyperlink C1Flexgrid Column Data in silverlight

I am using Component one FlexGrid in my silverlight Application and it is autogenerating columns in the grid. I want to make one of the column's data behave as a clickable hyperlink. Any help on this problem would be greatly appreciated.
I have figured out a way to add hyperlink cell in C1FlexGrid.
One should extend CellFactory Class and inside the class
override method CreateCellContent(C1FlexGrid grid, Border bdr, CellRange range)
and write something like this:
public override void CreateCellContent(C1FlexGrid grid, Border bdr, CellRange range)
{
//Ofcourse One should figure out first the col in which they want to
//add the cell
var width = GetWidthForHyperlinkControl((string)grid[range.Row, range.Column]);
var cell = new HyperlinkControl
{
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
Width = width,
Height = 16,
NavigateUri = null,
IsTabStop = false,
Content = (string)grid[range.Row, range.Column]
};
}
The sample projects for ComponentOne FlexGrid include a Hyperlink sample. Should be part of your installed items.
If not, you can also access it via the ComponentOne website.
Essentially, you set up a style for the hyperlink cells/columns and apply it. You can use OwnerDrawCell events to do it, as the example shows.

Resize DataGridView

I have a column that holds Checkboxes.
My main problem is that when I call this function:
dataGrid.AutoResizeColumn(0, DataGridViewAutoSizeColumnMode.DisplayedCells);
It stretches the checkbox column too, and I want that column to stay in 25 in width.
How can I do that? (only checkbox column not stretched)
Here is some more code, showing what I want to happen:
dataGrid.AutoResizeColumn(0, DataGridViewAutoSizeColumnMode.DisplayedCells);
dataGrid.AutoResizeColumn(1, DataGridViewAutoSizeColumnMode.Fill);
dataGrid.AutoResizeColumn(2, DataGridViewAutoSizeColumnMode.Fill);
dataGrid.AutoResizeColumn(3, DataGridViewAutoSizeColumnMode.DisplayedCells);
dataGrid.AutoResizeColumn(4, DataGridViewAutoSizeColumnMode.Fill);
dataGrid.AutoResizeColumn(5, DataGridViewAutoSizeColumnMode.Fill);
But the fill gives me an error.
What you are asking doesn't quite make sense, since the method you show AutoResizeColumn take as its first parameter the column index to resize - if you call this method with the index of the checkbox column then your are explicitly telling the grid to resize that column. If you don't want the resize, don't do that!
If you set the resize mode the next level up for the grid, you do it like this:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
Now with this method of setting the sizing mode then yes, the checkbox will change when you might want it to.
The answer to that is to use either the designer or the method you mention above, and set the checkbox columns autosize mode to None
If you absolutely must loop over the columns setting their AutoResize mode, then the only option you have is to check if you have the checkbox column in the loop and apply a different more.
This is in answer to your comment - the error you will be seeing is an ArgumentException being thrown by the AutoResizeColumn method. This is all documented on the MSDN page for the AutoResizeColumn method. You cannot specify a AutoSize mode of None or Fill.
It sounds like what you want to do is something like:
// If column 3 is the checkbox column, we set its resize mode to none:
dataGridView1.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
// Then we set the width:
dataGridView1.Columns[3].Width = 25;
// Finally we set the rest of the grid to fill or what ever resizing you need:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
you can define in datagridview_cellpainting event
private void gvDocumentList_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == gvDocumentList.Columns["checkbox column name"].Index && e.RowIndex >= 0)
{
e.PaintBackground(e.ClipBounds, true);
Rectangle rectRadioButton = new Rectangle();
rectRadioButton.Width = 14;
rectRadioButton.Height = 14;
rectRadioButton.X = e.CellBounds.X + (e.CellBounds.Width - rectRadioButton.Width) / 2;
rectRadioButton.Y = e.CellBounds.Y + (e.CellBounds.Height - rectRadioButton.Height) / 2;
e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus);
e.Handled = true;
}
}

Categories

Resources