2016-08-24 4 views
1

내 데이터베이스 TextBooks의 테이블에서 바인딩 된 데이터의 Gridview에 편집/업데이트 옵션을 추가했습니다. 관리자가 정보를 수정/업데이트 할 수있는 길을 변경하는 경우이를 허용합니다. 현재 나는 정보에서 편집 유형을 클릭 할 수 있으며 페이지의 현재 그리드에 바인드하지만 데이터베이스에 저장하지 않으며 페이지를 떠나서 돌아 오면 이전 페이지로 되돌아갑니다 값. BindGrid() 메소드에 대한 코드를 따라 한 후에 나는 어딘가 잘못되었지만 그것이 무엇인지를 완전히 파악할 수 없다고 생각한다.업데이트 된 Gridview 행을 데이터베이스 테이블에 저장

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!this.IsPostBack) 
     { 
      DataTable dt = new DataTable(); 
      string query = "select * from textBooks "; 
      using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HUTDMSConnectionString"].ToString())) 
      using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection)) 


       adapter.Fill(dt); 
      ViewState["allBooks"] = dt; 
      this.BindGrid(); 
     } 
    } 

    protected void BindGrid() 
    { 
     GridView1.DataSource = ViewState["allBooks"] as DataTable; 
     GridView1.DataBind(); 
    } 

    protected void OnRowEditing(object sender, GridViewEditEventArgs e) 
    { 
     GridView1.EditIndex = e.NewEditIndex; 
     this.BindGrid(); 
    } 

    protected void OnUpdate(object sender, EventArgs e) 
    { 
     GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow; 

     string Ancillary = (row.Cells[3].Controls[0] as TextBox).Text; 
     string BookActive = (row.Cells[4].Controls[0] as TextBox).Text;   
     string InactiveDate = (row.Cells[6].Controls[0] as TextBox).Text; 
     string Author = (row.Cells[7].Controls[0] as TextBox).Text; 
     string Publisher = (row.Cells[8].Controls[0] as TextBox).Text; 
     string EditionAndDate = (row.Cells[9].Controls[0] as TextBox).Text; 
     string Imprint = (row.Cells[10].Controls[0] as TextBox).Text; 
     string eISBN = (row.Cells[13].Controls[0] as TextBox).Text; 
     string ebookAvailable = (row.Cells[14].Controls[0] as TextBox).Text; 
     string Notes = (row.Cells[15].Controls[0] as TextBox).Text; 

     DataTable dt = ViewState["allBooks"] as DataTable; 

     dt.Rows[row.RowIndex]["Ancillary"] = Ancillary; 
     dt.Rows[row.RowIndex]["BookActive"] = BookActive; 
     dt.Rows[row.RowIndex]["InactiveDate"] = InactiveDate; 
     dt.Rows[row.RowIndex]["Author"] = Author; 
     dt.Rows[row.RowIndex]["Publisher"] = Publisher; 
     dt.Rows[row.RowIndex]["EditionAndDate"] = EditionAndDate; 
     dt.Rows[row.RowIndex]["Imprint"] = Imprint; 
     dt.Rows[row.RowIndex]["eISBN"] = eISBN; 
     dt.Rows[row.RowIndex]["ebookAvailable"] = ebookAvailable; 
     dt.Rows[row.RowIndex]["Notes"] = Notes; 


     ViewState["allBooks"] = dt; 
     GridView1.EditIndex = -1; 
     this.BindGrid(); 
    } 

답변

0

그래서 당신은 현재 다음의 GridView의 데이터를 편집, 데이터베이스에서 데이터를 당기는있는 gridview에 저장되어 무엇을하고 있는지. 해당 databind 명령을 호출하면 해당 데이터는 데이터베이스와 독립적이됩니다. 이 데이터를 데이터베이스에 저장하려면 OnUpdate 메서드에서 SqlDataAdapter의 Update method을 사용하는 것이 좋습니다. 이렇게하면 GridView를 채울 때와 유사한 전략을 따르는 동안 업데이트 값을 다시 데이터 행에 저장하고 데이터베이스에 저장할 수 있습니다.

+0

정직하게 내가하고있는 일에서 잃어 버렸습니다. 그저 Update 메소드에서 해당 페이지를 읽었고 내가 가지고있는 것과 정확히 통합하는 방법을 보지 못했습니다. – frostyBolt

관련 문제