2013-04-15 84 views
1

如何從gridview更新SQL數據庫? (更新記錄後)ASP.Net如何從gridview更新數據庫C#

使用 - SharePoint - >應用程序頁面 - 我知道,SharePoint ....「使基本的東西更加基礎和困難的事情不可能」(甚至沒有SQLDatasource控件一邊,你必須把它自己加入,如果你想它 - 不知道,如果他們不想讓你添加一個項目或東西...)

I have data being displayed in the gridview,並啓用了

AutoGenerateEditButton="True" 

在gridview中。 gridview的數據源是我從數據庫中填充的數據集。

我認爲沒有綁定到你的gridview的SQLDatasource,你將不得不再次連接到數據庫並手動更新數據庫。 但我似乎找不到任何方式來執行此操作,而不是綁定到自動更新GridView的SQLDataSource。

方法可以做到這一點,但需要在SqlDataSource綁定到GridView

Method Nr1

Method Nr2

你怎麼會去有關編輯在GridView後更新數據庫?

- 更新解決方案 - 添加了一些修改。由於

protected void Page_Load(object sender, EventArgs e) 
     { 
      int employeerId = 1; 
      //Add all the employees to the list to view them. 
      string query = "select emp.Name as [Employee Name], em.* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId where emp.managerId =" + employeerId; 

      DataSet ds = dbConn.returnSqlDataset(query); 
      grdViewDetails.DataSource = ds; 
      grdViewDetails.DataBind(); 
      int x = grdViewDetails.Columns.Count; 

      //Hide the columns. 
      grdViewDetails.DataBind(); 
      if (grdViewDetails.Columns.Count > 0) 
      { 
       grdViewDetails.Columns[2].Visible = false; 
       grdViewDetails.Columns[3].Visible = false; 
      } 
      else 
      { 
       grdViewDetails.HeaderRow.Cells[0].Visible = false; 
       foreach (GridViewRow gvr in grdViewDetails.Rows) 
       { 
        gvr.Cells[2].Visible = false; 
        gvr.Cells[3].Visible = false; 
       } 
      } 
     } 


protected void grdViewDetails_RowEditing1(object sender, GridViewEditEventArgs e) 
    { 
     string query = "select emp.Name as [Employee Name], em.* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId where emp.managerId =" + 1; 
     grdViewDetails.EditIndex = e.NewEditIndex; 
     //e.newedit index:- will be provide index of row for which edit button is selected 
     grdViewDetails.DataSource = dbConn.returnSqlDataset(query); 
     grdViewDetails.DataBind(); 
    } 



    protected void grdViewDetails_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     string query = "select emp.Name as [Employee Name], em.* from EthicsManagement em join EmployeeTable emp on em.employeeId = emp.employeeId where emp.managerId =" + 1; 

     foreach (TableCell cell in grdViewDetails.Rows[e.RowIndex].Cells) 
     { 
      if (cell.Controls[0] is TextBox) 
      { 
       TextBox textbox = (TextBox)cell.Controls[0]; 
       string value = textbox.Text; 
      } 
      else 
      { 
       if (cell.Controls[0] is CheckBox) 
       { 
        CheckBox chkBoxWeek = (CheckBox)cell.Controls[0]; 
        Boolean checkStatus = chkBoxWeek.Checked; 
       } 
      } 
     } 

回答

1

行編輯: 當用戶點擊編輯按鈕後backtakes地方,網格視圖的列編輯事件將executed.This活動將提供行索引

protected void gremp_rowediting(object sender,EventArgs e) 
{ 
    dataset ds=(dstaset)session["ds1"]; 
    gvemp.editindex=e.new editindex; 
//e.newedit index:- will be provide index of row for which edit button is selected 
    gvemp.Datasource=Ds.Table["employee"]; 
    gvemp.Databind(); 
} 

行更新: 這將執行回發,將執行網格視圖的行更新事件過程

protected void gvemp-rowupdating(Object sender,EvenArgs e) 
{ 
    Textbox txt=(Textbox)gvemp.Rows[e.RowIndex].cells[3].controls[0]; 
//here i will update the third cell data in grid view 
    int avg=int.parse(txt.Text); 
    Dataset ds=(dataset)session["ds1"]; 
    ds.Tables["employee"].rows[e.Rowindex]["Average"]=avg; 
    ds.Tables["employee"].AcceptChanges(); 
    session["Ds1"]=ds; 
//it will overwrite the session of Dataset 
//Rearrange Gridview 
    gvemp.editIndex=-1; 
    gvemp.Datasource=Ds.Tables["employee"]; 
    gvemp.DataBind(); 
} 

請點擊此鏈接查看更多d etails:

http://www.aspdotnet-sharepoint.com/2011/11/row-editdeleteupdate-in-grid-view-using.html

2

行編輯:當用戶點擊編輯按鈕和網格視圖的行編輯事件將executed.This事件將提供行索引

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
    { 
     GridView1.EditIndex = e.NewEditIndex; 
     databind(); 
    } 

行更新:這將執行回發,網格視圖的行更新事件過程將被執行

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     Label l = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1"); 
     TextBox t1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1"); 
     TextBox t2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2"); 
     FileUpload fu = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1"); 

     string fpath = Server.MapPath("images"); 
     string fname = fu.FileName; 
     string concat = fpath + "\\" + fname; 
     fu.SaveAs(concat); 

     cmd = new SqlCommand("update userdata set username='" + t1.Text + "', password='" + t2.Text + "' , Image = '" + "~/images/"+ fu.FileName + "' where userid='" + Convert.ToInt32(l.Text) + "'", con); 

     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     GridView1.EditIndex = -1; 
     databind(); 
    }