如何自动结束DataGridView单元格编辑模式? [英] How to end the DataGridView Cells Edit Mode automatically?

查看:477
本文介绍了如何自动结束DataGridView单元格编辑模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前的一个后续问题。你可以找到它
MSDN文章如何设置绑定到窗体控件从Visual Studio。最后一节描述你在做什么。最后的句子DataGridView控件现在绑定到您已经拖动到其上的表,DataSet,TableAdapter和BindingSource出现在组件托盘中。是重要的。由于Visual Studio生成的代码将您的控件绑定到您的表上,您应该直接编辑数据以更新数据网格视图。在这种情况下,您应该使用生成的DataSet(我假定它被命名为surat_jalanDataSet)。 这里是如何在DataSet中编辑数据的说明。对于您的具体情况,请添加行。请让我知道,如果这有助于您达到目标。


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? 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

解决方案

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.

这篇关于如何自动结束DataGridView单元格编辑模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆