2014-04-22 10 views
0

GridView가 있고 ObjectDataSource의 사용자 정의 메서드를 사용하여 행을 편집/업데이트하고 싶습니다. 자동 생성 된 열을 사용하지만 BoundFields는 필요하지 않습니다. GridView1.DataBind() RowUpdated, RowUpdating 및 RowEditing 이벤트 일을 시도했다. 오류 메시지가 표시되지 않습니다. 행을 업데이트하지 않습니다.ObjectDataSource를 사용하는 GridView BoundField

IncidentUpdate.aspx

<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2" AutoGenerateColumns="false"> 
    <Columns> 
     <asp:CommandField ShowEditButton="True" /> 
     <asp:BoundField DataField="IncidentID" HeaderText="ID" ReadOnly="True" /> 
     <asp:BoundField DataField="ProductCode" HeaderText="Product Code" ReadOnly="True" /> 
     <asp:BoundField DataField="DateOpened" HeaderText="Date Opened" ReadOnly="True" /> 
     <asp:BoundField DataField="DateClosed" HeaderText="Date Closed" /> 
     <asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" /> 
     <asp:BoundField DataField="Description" HeaderText="Description" /> 
    </Columns> 
</asp:GridView> 
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetCustomerIncidents" TypeName="IncidentDB" UpdateMethod="UpdateIncident"> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="DropDownList1" Name="customerID" PropertyName="SelectedValue" Type="Int32" /> 
    </SelectParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="incidentID" Type="Int32" /> 
     <asp:Parameter Name="dateClosed" Type="DateTime" /> 
     <asp:Parameter Name="description" Type="String" /> 
    </UpdateParameters> 
</asp:ObjectDataSource> 

을 App_Code \ Service.cs

public static DataSet UpdateIncident(int incidentID, DateTime dateClosed,string description) 
{ 
    TechSupportDB techSupportDB = new TechSupportDB(); 
    var myConnection = techSupportDB.GetConnectionString(); 

    SqlCommand myCommand = new SqlCommand(); 

    myCommand.Parameters.AddWithValue("@IncidentID", incidentID); 
    myCommand.Parameters.AddWithValue("@DateClosed", dateClosed); 
    myCommand.Parameters.AddWithValue("@Description", description); 
    myCommand.CommandText = 
    "update Incidents set DateClosed = @DateClosed, Description = @Description where IncidentID = @IncidentID"; 

    myCommand.Connection = myConnection; 
    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand); 
    DataSet updatedIncidentsDataSet = new DataSet(); 
    myAdapter.Fill(updatedIncidentsDataSet); 
    return updatedIncidentsDataSet; 
} 
+0

제목을 편집했습니다. "[제목에"태그 "가 포함되어 있어야합니까?] (http://meta.stackexchange.com/questions/19190/)"합의가 "아니오, 그렇지 않아야합니다"로 표시되어야합니다. –

답변

0

IncidentID의 BoundField는 readOnly로 설정되어 있으므로 사용자가이 필드를 편집 할 수 없지만이 필드도 업데이트를 수행하는 데 필요한 값을 전달하지 못합니다. 해결 방법 : DataKeyNames = IncidentID를 GridView에 설정합니다.

<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2" 
    AutoGenerateColumns="False" DataKeyNames="IncidentID"> 
0

난 당신이 정말 당신의 코드를 사용하여 테이블을 갱신하지 않습니다 생각합니다.

변경 여기에 코드의이 부분 :

public static DataSet UpdateIncident(int incidentID, DateTime dateClosed,string description) 
{ 
    TechSupportDB techSupportDB = new TechSupportDB(); 
    var myConnection = techSupportDB.GetConnectionString(); 

    SqlCommand myCommand = new SqlCommand(); 

    myCommand.Parameters.AddWithValue("@IncidentID", incidentID); 
    myCommand.Parameters.AddWithValue("@DateClosed", dateClosed); 
    myCommand.Parameters.AddWithValue("@Description", description); 
    myCommand.CommandText = 
    "update Incidents set DateClosed = @DateClosed, Description = @Description where IncidentID = @IncidentID"; 

    myCommand.Connection = myConnection; 
    SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT * FROM Incidents ORDR BY IncidentId"); 

    DataSet updatedIncidentsDataSet = new DataSet(); 
    myAdapter.Fill(updatedIncidentsDataSet, "Incidents"); 

    myAdapter.UpdateCommand = myCommand; 
    myAdapter.Update(updatedIncidentsDataSet, "Incidents"); 

    return updatedIncidentsDataSet; 
} 

당신은 업데이트중인 TABLENAME을 지정해야합니다. 귀하의 경우입니다 Incidents

시도 :

myAdpter.Update(updatedIncidentsDataSet, "Incidents"); 

우리는 우리가 당신이 Update unable to find TableMapping['Table']

을 받고 왜 그 "Table" 인 디폴트 테이블 이름을 사용하는 데이터 어댑터를 업데이트하는 테이블 이름을 지정하지 않으면
+0

행운이 없습니다. 나는 같은 결과를 얻는다. 업데이트되지 않으며 오류 메시지가 없습니다. – user2665267

+0

내가 게시 한 메소드 안에 중단 점을 설정해야 모든 행에 무엇이 있는지 확인할 수 있습니다. – jomsk1e

+0

이 줄에서 : myAdapter.Update (updatedIncidentsDataSet); 잘못된 작업 예외가 발생합니다. 업데이트가 TableMapping [ 'Table'] 또는 DataTable 'Table'을 (를) 찾을 수 없습니다. 데이터 세트에 테이블을 추가 한 다음 myAdapter.Update (updatedIncidentDataSet, "TableName")를 실행하려고 시도했지만 작동하지 않습니다. – user2665267

0

안녕하세요 저는 처음로드 할 때 원하는 결과 (원하는 결과)를 얻고 있다는 귀하의 질문에서 몇 가지를 얻고 있습니다. 그런 다음 (아마도 행을 추가, 편집 또는 삭제) 무언가를하고 있으며 어떤 작업을 어떤 행에서하고 있는지 알 수 있습니다.

그런 다음 별도로 해당 활동을 수행하는 별도의 기능을 작성하십시오. 새 행을 업데이트하는 경우 DB에서 업데이트하고 그리드를 데이터베이스에서 다시 채 웁니다.

 

    TechSupportDB techSupportDB = new TechSupportDB(); 
    var myConnection = techSupportDB.GetConnectionString();  
    SqlCommand myCommand = new SqlCommand();  
    myCommand.Parameters.AddWithValue("@IncidentID", incidentID); 
    myCommand.Parameters.AddWithValue("@DateClosed", dateClosed); 
    myCommand.Parameters.AddWithValue("@Description", description); 
    myCommand.CommandText = 
    "update Incidents set DateClosed = @DateClosed, Description = @Description where IncidentID = @IncidentID"; 
    myCommand.Connection = myConnection; 
    myCommand.ExecuteNonQuery(); 

이렇게하면 데이터를 DB 테이블에 저장하고 기본 쿼리에서 그리드를 다시 채 웁니다. 이 방법으로 원하는 결과를 얻을 수 있습니다.