Datagridview: How to set a cell in editing mode?
Setting the CurrentCell
and then calling BeginEdit(true)
works well for me.
The following code shows an eventHandler for the KeyDown
event that sets a cell to be editable.
My example only implements one of the required key press overrides but in theory the others should work the same. (and I'm always setting the [0][0] cell to be editable but any other cell should work)
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && dataGridView1.CurrentCell.ColumnIndex == 1)
{
e.Handled = true;
DataGridViewCell cell = dataGridView1.Rows[0].Cells[0];
dataGridView1.CurrentCell = cell;
dataGridView1.BeginEdit(true);
}
}
If you haven't found it previously, the DataGridView FAQ is a great resource, written by the program manager for the DataGridView control, which covers most of what you could want to do with the control.
Change datagridview cell value in edit mode
When the cell is in edit mode you need to change the text in the edit control, usually a Textbox. You can get (and hold) a handle to it in the EditingControlShowing
event:
TextBox editBox = null;
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox) editBox = e.Control as TextBox;
}
But using the CellEnter
event is not a good idea as it will be called when scrolling or clicking around as well..
To catch the beginning of editing you use the BeginEdit
event:
int yourEditColumn = 5;
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex == yourEditColumn )
{
string yourValue = "12345";
dataGridView1.Rows[e.RowIndex].Cells[yourEditColumn ].Value = yourValue;
if (editBox != null) editBox.Text = yourValue;
}
}
Different format for datagridview cell in edit mode
You need to apply the actual formatting and then set e.FormattingApplied
to True.
void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == colText.Index)
{
DataGridViewCell cell = dgv.Item[e.ColumnIndex, e.RowIndex];
if (cell.IsInEditMode)
{
e.Value = e.Value.ToString();
}
else
{
e.Value = e.Value.ToString("#,##0");
}
e.FormattingApplied = true;
}
}
Setting datagridview in editing mode
You need to use the Enter
event of the DataGridView
.
I added a simple DataGridView
to my form and added its Enter
event. Here's my testing code:
public Form1()
{
InitializeComponent();
// empty row and column so you have something to edit
dataGridView1.Columns.Add("col1", "Column 1");
dataGridView1.Rows.Add();
}
private void dataGridView1_Enter(object sender, EventArgs e)
{
dataGridView1.Rows[0].Cells[0].Selected = true;
dataGridView1.BeginEdit(false);
}
If your DataGridView
already has a data source, you need to be careful about the data type of the first column and the data that will be typed.
How to end the DataGridView Cells Edit Mode automatically?
Sorry about the delay. After seeing how you set up your binding to your DataGridView I can definately give you better guidance on how to edit the data that the grid is bound to. When you dragged the table from the data sources view in Visual Studio and dropped it on the DataGridView, Visual Studio did several things for you. It is important that you at least understand the basics of what was done so that you understand how you can manipulate you data moving forward. This
MSDN article how to setup binding to windows forms controls from Visual Studio. The last section describes what you are doing. The last sentences "The DataGridView control is now bound to the table that you have dragged onto it. A DataSet, TableAdapter, and BindingSource appear in the component tray." are the important ones. Since Visual Studio generated code that bound your control to your table you should edit the data directly to update your data grid view. In this case you should work with the generated DataSet (I'm assuming it was named surat_jalanDataSet). Here is a description of how to edit data in a DataSet. For your specific case Adding Rows. Please let me know if this helps you acheive your goals.
Enable edit mode in datagridview only for certain column c#
this will help you
MSDN DataGridViewColumn
Related Topics
A Dictionary Object That Uses Ranges of Values for Keys
C# Winforms: How to Set Main Function Stathreadattribute
How to Bind Datatable to Datagridview in C#
Get the Id of Inserted Row Using C#
Why Do Members of a Static Class Need to Be Declared as Static? Why Isn't It Just Implicit
Is There a C# Generic Constraint for "Real Number" Types
Generic C# Code and the Plus Operator
Redirecting Output to the Text File C#
Which Blocking Operations Cause an Sta Thread to Pump Com Messages
Using Tfs API, How to Find the Comments Which Were Made on a Code Review
How to Convert JSON Array to List of Objects in C#
Interesting "Params of Ref" Feature, Any Workarounds
How to Implement Automatic Sorting of Datagridview
Possible Pitfalls of Using This (Extension Method Based) Shorthand