How to end the DataGridView Cells Edit Mode automatically?

23,448

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.

Share:
23,448
Aurelius Anugrah Sugianto
Author by

Aurelius Anugrah Sugianto

just graduated and become a C# developer. but I'm still a beginner tho

Updated on September 27, 2020

Comments

  • Aurelius Anugrah Sugianto
    Aurelius Anugrah Sugianto over 3 years

    this is a follow up question from my previous one. you can find it here.

    I got another problem after finally solving my previous one, when I assign the button to add the value into a new row in DataGrid, the entire cell will be on edit mode, until I clicked on other cell and filled it and/or tabbed till the end of the row (well apparently this one doesn't work), then it will end the edit mode. I'm using dataGridView.BeginEdit(true); to begin the edit mode so that I could parse a value into the textbox (see my previous question). So, if I insert another value and press the button, the new value will replace the old value that was inserted previously because it is currently still on edit mode. I was trying to use dataGridView.EndEdit();, dataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);, cell.DataGridView.EndEdit() and cell.DataGridView.EndEdit(DataGridViewDataErrorContexts.Commit); but apparently that doesn't end the edit mode :( What I wanted is when I press the button, the value inside the textbox will be inserted into the first textbox column (this one already worked). Then I don't have to click or fill the other column to end the edit mode. So, I'll just type anything into the textbox and press the button only until I wanted to stop. After that I begin to fill the other column. does anyone know how to solve this? sample image EDIT 1: do you see the difference? look at the red circle, the top one is currently in edit mode (because it has a * after the arrow). the bottom one is not in edit mode (I did it manually by picking an item from the combobox). Here is my code as requested from my previous question:

    private void button1_Click(object sender, EventArgs e)
        {
            this.surat_jalanDataGridView.AllowUserToAddRows = true;
            string tokNum = this.textBox1.Text;
    
            if (this.textBox1.Text != "")
            {
                foreach (DataGridViewRow sjRow in this.surat_jalanDataGridView.Rows)
                {
                    int RowIndex = surat_jalanDataGridView.RowCount - 1;
                    DataGridViewRow R = surat_jalanDataGridView.Rows[RowIndex];
    
                    DataTable table = new DataTable();
                    DataRow newRow = table.NewRow();
                    table.Rows.Add(newRow);
    
                    DataGridViewCell cell = R.Cells[2];
                    this.surat_jalanDataGridView.CurrentCell = cell;
                    this.surat_jalanDataGridView.BeginEdit(true);
    
                    R.Cells[2].Value = tokNum;
                    cell.DataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
                }
            }
            this.surat_jalanDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
        }
    

    EDIT 2: So, I drag and drop the surat_jalan from the data sources into my windows form. then it automatically become a datagrid with the property name surat_jalanDataGridView and the data source is surat_jalanBindingSource img 2