0

我有一个带有 datagridview 的 crud 表单。这是我的“刷新”代码:

private void Recuperar()
        {
            DataSet ds = new DataSet();
            string CommandText = "SELECT * from sectores order by sector";
            SQLiteDataAdapter adapter = new SQLiteDataAdapter(CommandText, Variables.Conexion);
            DataTable table = new DataTable();
            table.TableName = "sectores";
            adapter.Fill(table);

            grilla.DataSource = table;

            grilla.Refresh();
            grilla.Columns["sector_id"].Visible = false;
            grilla.Columns["clave"].Visible = false;
            grilla.Columns["sector"].HeaderText = "Sector";
            grilla.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            SetDataBinding();

        }

private void SetDataBinding()
        {
            this.textBoxId.DataBindings.Clear();
            this.textBoxId.DataBindings.Add(new System.Windows.Forms.Binding("Text", grilla.DataSource, "sector_id"));
            this.tbSector.DataBindings.Clear();
            this.tbSector.DataBindings.Add(new System.Windows.Forms.Binding("Text", grilla.DataSource, "sector"));
            this.tbClave1.DataBindings.Clear();
            this.tbClave1.DataBindings.Add(new System.Windows.Forms.Binding("Text", grilla.DataSource, "clave"));
            this.tbClave2.DataBindings.Clear();
            this.tbClave2.DataBindings.Add(new System.Windows.Forms.Binding("Text", grilla.DataSource, "clave"));
        }

在“添加”按钮的代码中,我清除了控件的文本(绑定到数据网格的控件),因此用户插入了新值。

private void btn_agregar_Click(object sender, EventArgs e)
        {
            grilla.ClearSelection();
            accion = DataRowAction.Add;
            SetEstado();
        }

private void SetEstado()
        {
            if (accion == DataRowAction.Add)
                btn_guardar.Text = "Agregar";
            if (accion == DataRowAction.Change)
                btn_guardar.Text = "Modificar";
            if (accion == DataRowAction.Delete)
                btn_guardar.Text = "Eliminar";
            if (accion == DataRowAction.Nothing)
                btn_guardar.Text = "Guardar";


            groupBoxControles.Enabled = accion == DataRowAction.Add || accion == DataRowAction.Change;
            PanelDerechaBotones.Enabled = accion != DataRowAction.Nothing;
            panelAbajo.Enabled = accion == DataRowAction.Nothing;
            grilla.Enabled = accion == DataRowAction.Nothing;


            if (accion == DataRowAction.Add)
            {
                textBoxId.Text = "0";
                tbSector.Text = "";
                tbClave1.Text = "";
                tbClave2.Text = "";
            }

            if (accion == DataRowAction.Add || accion == DataRowAction.Change)
            {
                tbSector.Focus();
            }

        }

问题是当用户取消此操作时,在网格中选择的行现在看起来都是空的。(将空值从控件移到网格中)

我知道我错过了一些东西,但不知道是什么。感谢有人帮助我。

取消按钮代码

private void btn_cancelar_Click(object sender, EventArgs e)
        {
            accion = DataRowAction.Nothing;
            SetEstado();
            //((DataTable)grilla.DataSource).RejectChanges();
        }
4

1 回答 1

0

我假设取消按钮正在发回并且您没有重新绑定?网格必须在每次回发后反弹。

于 2013-07-26T12:59:22.693 回答