2

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

Community
  • 1
  • 1
  • you could also use `this.Validate` on your winform – Gabriel GM Jan 05 '12 at 03:18
  • I already tried it Gabriel, but that doesn't work either. but thanks anyway :D – Aurelius Anugrah Sugianto Jan 05 '12 at 03:41
  • Why do you use beginedit when you're always calling endedit right after. You can just set your value without going into edit mode. Edit mode is for the user to enter an input. – Gabriel GM Jan 05 '12 at 04:18
  • hmmm, I tried to remove the beginedit and endedit from the code. And surprisingly it doesn't make any difference :( at this point, beginedit and endedit is useless. – Aurelius Anugrah Sugianto Jan 05 '12 at 04:31
  • what is the `datasource` of your `DataGridView`? You're currently adding your new row to a new `DataTable` which make no sense. You should add to the current datasource. – Gabriel GM Jan 05 '12 at 12:46
  • the datasource is `surat_jalanBindingSource1`. But when I tried the code below from Kerry, if I change `private DataTable m_MyDataSource = new DataTable();` into `private DataTable surat_jalanBindingSource1 = new DataTable();` it throws an error `The type "FormAdd" already contains the definition for surat_jalanBindingSource1`. – Aurelius Anugrah Sugianto Jan 06 '12 at 01:52
  • Can you post in your answer your code where you declare and bind your DataTable to your BindingSource and to the DataGridView. It'll help a lot. – Gabriel GM Jan 06 '12 at 02:46
  • @GabrielGM & KerryH please look at the picture above. thanks – Aurelius Anugrah Sugianto Jan 06 '12 at 03:36
  • This clears things up a lot, I will updated my answer later today when I have more time to respond – Kerry H Jan 06 '12 at 15:29
  • @KerryH could you help me with my other questions Kerry? [please help me solve this problems](http://stackoverflow.com/questions/9222443/how-to-get-an-exact-matching-value-from-textbox-datagridviewcombobox-item-coll) thanks – Aurelius Anugrah Sugianto Feb 15 '12 at 02:16

2 Answers2

1

My work around to force a Cell out of EditMode is to toggle the CurrentCell.ReadOnly property.

this.dataGridView.EndEdit();
this.dataGridView.CurrentCell.ReadOnly = !dataGridView.CurrentCell.ReadOnly;
this.dataGridView.CurrentCell.ReadOnly = !dataGridView.CurrentCell.ReadOnly;

Similar technique works with other components that do not have explicit and functional EndEdit or Commit type methods.

For example, you can end the DateTimePicker's text edit mode by toggling its dateTimePicker.Enabled property.

//AJ

gridtrak
  • 731
  • 7
  • 20
1

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.

Kerry H
  • 661
  • 5
  • 7