2013-03-15 85 views
4

我正在c#窗體中的datagridview應用程序,我正在從數據庫加載數據,現在我希望用戶能夠編輯單元格值並將值保存到數據庫,如何編輯單元格的值以及如何將值保存到數據庫?datagridview單元格在Windows窗體中編輯和保存功能?

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"]; 
+2

請注意,「如何處理正在編輯的DataGridView」單元「和」如何將值保存到數據庫「是兩個獨立的問題。 – 2013-03-15 04:41:37

回答

0

看看DataGridView Events列表。您需要訂閱適當的事件並進行相應的處理。也就是說,你對DataGridView.CellValueChanged感興趣。

dataGridView1.CellValueChanged += ValueChangedHandler; 

private void ValueChangedHandler(object sender, DataGridViewCellEventArgs e) { 
    // do what is appropriate here. 
} 
5

一個更新與DataGridView數據庫使用的DataGridView的事件的方式:

DataGridView.CellBeginEdit 

DataGridView.CellValidating 

DataGridView.CellEndEdit 

讓說:private DataGridView dgv; 添加事件的處理程序

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 
} 
1

嘗試這樣做:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) 
      { 
       //after you've filled your ds, on event above try something like this 
       try 
       { 

        da.Update(ds); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 
1

我使用OleDb,但可以使用SQL。這是我通常使用的代碼。

OleDbCommand sCommand; 
    OleDbDataAdapter sAdapter; 
    OleDbCommandBuilder sBuilder; 
    OleDbConnection connection; 
    DataSet sDs; 
    DataTable sTable; 
    string myMode = ""; 
    private void BtnLoad_Click(object sender, EventArgs e) 
    { 
     string query = "SELECT * FROM [Table]"; 
     connection = new OleDbConnection(connectionString); 
     connection.Open(); 
     sCommand = new OleDbCommand(query, connection); 
     sAdapter = new OleDbDataAdapter(sCommand); 
     sBuilder = new OleDbCommandBuilder(sAdapter); 
     sDs = new DataSet(); 
     sAdapter.Fill(sDs, "Table"); 
     sTable = sDs.Tables["Table"]; 
     connection.Close(); 
     DataGrid.DataSource = sTable; 
     DataGrid.ReadOnly = true; 
     DataGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 
    } 
    private void BtnAdd_Click(object sender, EventArgs e) 
    { 
     DataGrid.ReadOnly = false; 
     myMode = "add"; 
    } 
    private void BtnEdit_Click(object sender, EventArgs e) 
    { 
     DataGrid.ReadOnly = false; 
     myMode = "edit"; 
    } 
    private void BtnDelete_Click(object sender, EventArgs e) 
    { 
     myMode = ""; 
     if (MessageBox.Show("Do you want to delete this row ?", "Delete",  MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
     { 
      DataGrid.Rows.RemoveAt(DataGrid.SelectedRows[0].Index); 
      sAdapter.Update(sTable); 
     } 
    } 
    private void BtnSave_Click(object sender, EventArgs e) 
    { 
     if (myMode == "add") 
     { 
      sAdapter.Update(sTable); 
      MessageBox.Show("Prices Are Successfully Added.", "Saved.", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 
     } 
     else if (myMode == "edit") 
     { 
      string query = "UPDATE Table_Name SET " + 
       "Column1 = '" + DataGrid.SelectedRows[0].Cells[0].Value.ToString() + "' ," + 
       "Column2 = " + DataGrid.SelecteddRows[0].Cells[1].Value.ToString() + ", " + 
       "WHERE CONDITION"; 
      connection = new OleDbConnection(connectionString); 
      connection.Open(); 
      sCommand = new OleDbCommand(query, connection); 
      sAdapter = new OleDbDataAdapter(sCommand); 
      sBuilder = new OleDbCommandBuilder(sAdapter); 
      sDs = new DataSet(); 
      sAdapter.Fill(sDs, "Table"); 
      sTable = sDs.Tables["Table"]; 
      connection.Close(); 
      DataGrid.DataSource = sTable; 
      DataGrid.ReadOnly = true; 
     } 
    } 
相關問題