c#数据绑定修改网格值

时间:2013-07-26 12:51:28

标签: c# grid controls

我有一个带有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();
        }

1 个答案:

答案 0 :(得分:0)

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