datagridview单元格编辑和保存窗体中的功能? [英] datagridview cell edit and save functionality in windows forms?

查看:133
本文介绍了datagridview单元格编辑和保存窗体中的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#windows窗体应用程序中处理datagridview,并且正在从数据库中加载数据,现在我希望用户能够编辑单元格值并将值保存到数据库中,如何编辑单元格值,如何将值保存到数据库?

  SqlConnection con = new SqlConnection(user id = sa ; password = 123; database = employee); 
SqlDataAdapter da = new SqlDataAdapter(select * from UserReg,con);
DataSet ds = new DataSet();
da.Fill(ds,p);
dataGridView1.DataSource = ds.Tables [p];


解决方案

使用 DataGridView 正在使用DataGridView的事件:

  DataGridView.CellBeginEdit 

DataGridView.CellValidating

DataGridView.CellEndEdit

让我们说: code> private DataGridView dgv;
添加事件处理程序

  dgv。 CellBeginEdit + = dgv_CellBeginEdit; 
dgv.CellValidating + = dgv_CellValidating;
dgv.CellEndEdit + = dgv_CellEndEdit;

private void dgv_CellBeginEdit(Object sender,DataGridViewCellCancelEventArgs e)
{
//这里我们将一个单元格的当前值保存到某个变量中,以后我们可以与一个新值进行比较
//例如使用dgv.Tag属性
if(e.RowIndex> = 0&& e.ColumnIndex> = 0)
{
this。 dgv.Tag = this.dgv.CurrentCell.Value;
//或将发件人转发给DataGridView变量 - >比此处理程序可以在另一个datagridview中使用
}
}

private void dgv_CellValidating(Object sender,DataGridViewCellValidatingEventArgs e)
{
//在这里可以为新值添加所有类型的检查
//对于exapmle简单比较旧值并检查是否超过0
if(this.dgv.Tag = this.dgv.CurrentCell.Value)
e.Cancel = true; //取消当前单元格的更改
//例如使用的整数检查
int32 iTemp;
if(Int32.TryParse(this.dgv.CurrentCell.Value,iTemp)= True&& iTemp> 0)
{
// value is ok
}
else
{
e.Cancel = True;
}
}

私有子dgvtest1_CellEndEdit(Object sender,DataGridViewCellEventArgs e)
{
//因为CellEndEdit事件发生在CellValidating事件之后(如果未取消) )
//在这里您可以将新值更新为


I'm working on datagridview in c# windows forms application and I'm loading the data from the database, now i want the user to be able to able to edit the cell value and save the value to the database, how to edit the cell value and how can i save the value to the database?

  SqlConnection con = new SqlConnection("user id=sa;password=123;database=employee");
  SqlDataAdapter da = new SqlDataAdapter("select * from UserReg", con);
  DataSet ds = new DataSet();
  da.Fill(ds, "p");
  dataGridView1.DataSource = ds.Tables["p"];

解决方案

One of the way to update a database with DataGridView is using of DataGridView's events:

DataGridView.CellBeginEdit

DataGridView.CellValidating

DataGridView.CellEndEdit

Let say: private DataGridView dgv; Add handlers of events

dgv.CellBeginEdit += dgv_CellBeginEdit;
dgv.CellValidating += dgv_CellValidating;
dgv.CellEndEdit += dgv_CellEndEdit;

private void dgv_CellBeginEdit(Object sender, DataGridViewCellCancelEventArgs e)
{
     //Here we save a current value of cell to some variable, that later we can compare with a new value
    //For example using of dgv.Tag property
    if(e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        this.dgv.Tag = this.dgv.CurrentCell.Value;
        //Or cast sender to DataGridView variable-> than this handler can be used in another datagridview
    }
}

private void dgv_CellValidating(Object sender, DataGridViewCellValidatingEventArgs e)
{
    //Here you can add all kind of checks for new value
    //For exapmle simple compare with old value and check for be more than 0
    if(this.dgv.Tag = this.dgv.CurrentCell.Value)
        e.Cancel = true;    //Cancel changes of current cell
    //For example used Integer check
    int32 iTemp;
    if (Int32.TryParse(this.dgv.CurrentCell.Value, iTemp) = True && iTemp > 0)
    {
        //value is ok
    }
    else
    {
        e.Cancel = True;
    }
}

Private Sub dgvtest1_CellEndEdit(Object sender, DataGridViewCellEventArgs e)
{
    //Because CellEndEdit event occurs after CellValidating event(if not cancelled)
    //Here you can update new value to database
}

这篇关于datagridview单元格编辑和保存窗体中的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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