I am using the following code so that gridview cell support multiline while in edit mode :
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
((DataGridViewTextBoxEditingControl)e.Control).AcceptsReturn = true;
}
when I press SHIFT+ ENTER ,gridviewcell provides newline but whole content of cell is not displayed as current line moves up and displays only one line at a time at which your text caret is blinking.
Is there anyway I can show whole editing cell with multiline while in edit mode itself?
try this....
The best way is by handling the EditingControlShowing event of the grid and add the following code
if ((e.Control.GetType() == TextBox))
{
TextBox txtB = new TextBox();
txtB = e.Control;
txtB.Multiline = true;
txtB.ScrollBars = ScrollBars.Both;
}
Related
I have implemented a DataGridViewComboBox that allows editing using the code from this thread.
My problem right now is that when I focus the cell, the value is cleared. Based on the posts I've read, the item must be added to the combobox first. So here's what I have tried. Can you possibly tell me what's going wrong?
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
DataGridViewRow row = dataGridView1.CurrentRow;
DataGridViewCell cell = dataGridView1.CurrentCell;
if (cell == row.Cells[colComboBox.Name])
{
DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl;
control.DropDownStyle = ComboBoxStyle.DropDown;
//For testing purposes
colComboBox.Items.Add("Test");//I'm adding the item to the combobox control
row.Cells[colComboBox.Name].Value = "Test";//Then set the value of the cell based on the item I added
}
}
}
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == colComboBox.Index)
{
colComboBox.Items.Clear();
object eFV = e.FormattedValue;
if (!colComboBox.Items.Contains(eFV))
{
colComboBox.Items.Add(eFV);
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = eFV;
}
}
}
In the example you linked, the Validating event used is the editing combo box one and not the DataGridView's CellValidating one.
By the way, using the EditingControlShowing event to access the editing control is generally a bad idea. I would suggest you to use a cell template by adding this code before populating your datagridview :
DataGridViewComboBoxCell templateCell = new DataGridViewComboBoxCell();
templateCell.Items.Add("Test");
templateCell.Items.Add("Test2");
colComboBox.CellTemplate = templateCell;
I'm not entirely sure why the DataGridViewComboBox behaves this way. But experimenting on to the ComboBox control with default properties, it does not clear the value when focused and behaves just like a TextBox control. The solution is just to actually set the Text property of the control to a e.g. global variable of some sort that can be accessed by other events bound to the control. As for now this is the behaviour that I'm trying to achieve.
DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl;
control.DropDownStyle = ComboBoxStyle.DropDown;
control.Text = SomeGlobalVariable;
All,I knew we can set a column editable for a DataGridView.
And when finish editing the cell. the CellEndEdit event would be triggered.
But I just want to know why didn't end the edit of cell when I click the blank area of DataGridView. And click the area out of DataGridView doesn't trigger it too. only clicking the other cells could make it happen. It really doesn't make sense. Could anyone know why ? and How to make it ? It try to use the Click event of the DataGridView, But When I click the cell, It also trigger the DataGridView_click event.
private void dgvList_Click(object sender, EventArgs e)
{
dgvFileList.EndEdit();
}
Try using the HitTest function in the MouseDown event of the grid:
void dgvFileList_MouseDown(object sender, MouseEventArgs e) {
DataGridView.HitTestInfo hit = dgvFileList.HitTest(e.X, e.Y);
if (hit.RowIndex < 0 | hit.ColumnIndex < 0) {
dgvFileList.EndEdit();
}
}
Clicking outside the DataGridView control would require hitting a focusable control.
Before BeginEdit. Set a variable to identify if current state is edit mode.
bBeginEdit = true;
dgvFileList.BeginEdit(false);
In the Form_Click event
if (bBeginEdit)
{
dgvFileList.EndEdit();
bBeginEdit = false;
}
Thanks,
Joe
CellEndEdit() causes the event to be fired only if the cell was in edit mode (see Joe.wang's response). You can simply preceed CellEndEdit() with CellBeginEdit() to enter Edit mode (code from CellContentClick-handler, PickNewFont() is a wrapper for the FontDialog):
[...]
else if (String.Compare(rowName, "Font name") == 0) // user clicks on Font-row
{
dgvConfigSettings.BeginEdit(true);
Font newFont = PickNewFont(fontName, fontSize, fontStyle);
dgvConfigSettings.CurrentCell.Tag = newFont; // a bit dirty.... but that way we can pick-up the font in the panel-handler more easily
dgvConfigSettings.CurrentCell.Value = newFont.Name.ToString();
dgvConfigSettings.EndEdit();
}
I'm working on a C# winForm that uses a dataGridView that has a Column with multiple DataGridViewCell types. e.g.
DataGridView dvg = new DataGridView();
dgv.Columns.Add("colCtrl", "Ctrl");
DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
TextBox txtBox = new TextBox();
txtBox.Name = "MyTextBox";
txtBox.KeyDown += txtBox_KeyDown;
cboCell.Items.Add(txtBox);
DataGridViewRow row = new DataGridViewRow();
row.Cells.Add(cboCell);
dgv.Rows.Add(row);
private void txtBox_KeyDown(object sender, KeyEventArgs e)
{
DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell) ((TextBox)sender).Parent;
TextBox txtBox = (TextBox)sender;
if (e.KeyCode == Keys.Enter)
{
if (!cb.Items.Contains(txtBox.Text))
cb.Items.Add(txtBox.Text);
}
}
the textBox control is not being render to the comboBox Items collection. Its seems to be added when I step through code but It's not showing up. I'd basically like to use the textBox to add string items to the my comboBox dropDownList. What am I doing wrong here?
Thanks in advance,
-DA
Items in ComboBox are strings in default. In WPF, you can just add the controls to the ComboBox. In Winforms, you might need to customize your comboBox.
Trying to render different control types in a DataGridView is hard. DataGridView is meant to display data not so much control it.
From MSDN:
DataGridView: Displays data in a customizable grid.
I suggest you use the DataRepeater control if your version of C# has it
All I have to say is MSFT needs go back to the drawing board and revamp the dataGridView. Don't get me wrong it has a lot good functionality but its WAY to convoluted and lacks common features that you would expect the API to support! Seems like every time I work with the dataGridView I have to jump through hoops to accomplish the most simple of task, but anyway here's my hack job to render a textbox...keep in mind this is a HACK far from being the correct way to do this, oh well... hope this helps someone.
public class DropDownCellWithTextBox : DataGridViewComboBoxCell
{
ContextMenuStrip dropDownList;
ToolStripTextBox txtBox;
DataGridView dgv;
public DropDownCellWithTextBox(DataGridView _dgv)
{
dgv = _dgv;
dropDownList = new ContextMenuStrip();
txtBox = new ToolStripTextBox();
txtBox.BorderStyle = BorderStyle.FixedSingle;
txtBox.KeyDown += txtBox_KeyDown;
dropDownList.Items.Add(txtBox);
}
public override void InitializeEditingControl(int rowIndex,
object initialFormattedVaulue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex,initialFormattedValue,dataGridViewCellStyle);
ContextMenuStrip cms = DataGridView.EditingControl as ContextMenuStrip;
}
protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
//base.OnMouseClick(e)
dropDownList.Size = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Size;
dropDownList.Show(dgv.PointToScreen(new Point(e.X, e.Y));
}
private void txtBx_KeyDown(object sender, KeyEventArgs e)
{
ToolStripTextBox txt = (ToolStripTextBox)sender;
if (txt.Text == "")
return;
if (e.KeyCode == Keys.Enter)
{
ToolStripMenuItem tsmi = new ToolStripMenuItem(txt.Text);
tsmi.Name = txt.Text;
if (!dropDownList.Items.ContainsKey(tsmi.Name))
{
dropDownList.Items.Add(tsmi);
txt.Text = "";
}
}
}
}
I managed to create textboxes dynamically in C#. When a textbox has text, can I make it disappear when I click on it?
I need to put a word inside textboxes that is the result of a select in Oracle.
Assign a value to the Text property of the TextBox. You could subscribe then to the GotFocus event, and set the text value to an empty string.
// Holds a value determining if this is the first time the box has been clicked
// So that the text value is not always wiped out.
bool hasBeenClicked = false;
private void TextBox_Focus(object sender, RoutedEventArgs e)
{
if (!hasBeenClicked)
{
TextBox box = sender as TextBox;
box.Text = String.Empty;
hasBeenClicked = true;
}
}
javascript is good for the delete.
onclick="$(this).val('');"
alternatively you can use HTML5 placeholder
I have struggled all day trying to make my DataGridView behave. I'm using the following code to change a ComboBox cell to a TextBox, when user selects entry
It works beatifully for row 0, but for all other rows, the ComboBox remains on the screen, even though debugging says it's a TextBox cell! (inspecting CurrentCell).
Does anybody have a clue?
Here's a snippet:
void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
DataGridViewComboBoxEditingControl cb = sender as DataGridViewComboBoxEditingControl;
if (cb.SelectedIndex == 0 && dataGridViewReceivers.CurrentCell is DataGridViewComboBoxCell)
{
cb.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
dataGridViewReceivers.CellLeave -= new DataGridViewCellEventHandler(dataGridViewReceivers_CellLeave);
dataGridViewReceivers.EndEdit();
// Change to editing mode
int row = dataGridViewReceivers.CurrentCell.RowIndex;
dataGridViewReceivers[0, row] = new DataGridViewTextBoxCell();
dataGridViewReceivers[0, row].Value = "";
dataGridViewReceivers.BeginEdit(false);
dataGridViewReceivers.RefreshEdit();
dataGridViewReceivers.CellLeave += new DataGridViewCellEventHandler(dataGridViewReceivers_CellLeave);
cb.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
New rows are added using a BindingSource.AddNew().
Thanks!
EDIT:
When I call the code to replace the cell object, outside the event handler - it works! This indicates that it is worth trying a delegate...
EDIT:
Problem solved
It turns out that if I remove the focus from the DGV during the replacement, it works! Simply momentarily setting the focus to a button, and back again does the trick!
For some reason, the DGV does not allow me to change the cell type when it has focus. Unselecting the cell is not enough - focus needs to be removed from the control.
This looks like a .NET bug!...