2017-06-29 20 views
0

こんにちは、私はASPxGridviewに関していくつかの問題を抱えています。以下は私のコードはどこに私は正常にデータベースにレコードを更新することができます。しかし、フォームデータの編集で「更新」をクリックすると、正常に保存されますが、F5キーまたはページ再読み込みボタンを押すまで、aspxgridviewは自動的に更新されません。手動でリロードせずに変更を反映させるにはどうすればいいですか? "EditFormUpdateButton"をクリックするとjavascript関数を呼び出せますか?この問題を克服するために私を助けてください。DevExpress更新後に自動的にASPxGridViewを更新する

[ASPx] 
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolder1"> 

<script type="text/javascript"> 
    function onBindGridview(s, e) { 
     GridView1.PerformCallback("Reload"); 
    }  
</script> 

<dx:ASPxGridView ID="GridView1" ClientInstanceName="GridView1" runat="server" KeyFieldName="DeptId" AutoGenerateColumns="False" Width="100%" 
    OnRowUpdating="Gridview1_RowUpdating" OnCustomCallback="gridView_CustomCallback"> 
    <Columns> 
     <dx:GridViewCommandColumn ShowNewButtonInHeader="true" ShowEditButton="true" VisibleIndex="0" /> 
     <dx:GridViewDataTextColumn FieldName="DeptName" Name="Department" VisibleIndex="1"> 
      <EditFormSettings VisibleIndex="0" /> 
     </dx:GridViewDataTextColumn> 

    </Columns> 

    <SettingsEditing> 
     <BatchEditSettings EditMode="Row" /> 
    </SettingsEditing> 

    <SettingsPopup> 
     <EditForm Width="300" /> 
    </SettingsPopup> 

<Templates> 
     <EditForm>    
      <table> 
       <tr> 
        <td>Department Name : 
        </td> 
        <td> 

         <dx:ASPxTextBox runat="server" ID="edDeptName" Text='<%# Bind("DeptName") %>'> 
         </dx:ASPxTextBox>       
        </td> 
      </table> 

       <div style="text-align: left; padding: 2px"> 
         <dx:ASPxGridViewTemplateReplacement ID="UpdateButton" ReplacementType="EditFormUpdateButton" 
           runat="server" ></dx:ASPxGridViewTemplateReplacement>   
         <dx:ASPxGridViewTemplateReplacement ID="CancelButton" ReplacementType="EditFormCancelButton" 
          runat="server"></dx:ASPxGridViewTemplateReplacement> 
       </div>    
     </EditForm> 
    </Templates> 
</dx:ASPxGridView> 
</asp:Content> 

コードはC#ファイルの背後にある

[C#] 
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    {   
     BindDeapartment(); 
    } 
} 

public void BindDeapartment() 
{ 
    SqlConnection con = new SqlConnection(constr); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "get_department"; 
    cmd.Connection = con; 

    try 
    { 
     con.Open(); 
     GridView1.DataSource = cmd.ExecuteReader(); 
     GridView1.DataBind(); 
     GridView1.Columns[0].Visible = true; 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
    finally 
    { 
     con.Close(); 
     con.Dispose(); 
    } 
} 

protected void Gridview1_RowUpdating(object sender, ASPxDataUpdatingEventArgs e) 
{ 

    SqlConnection con = new SqlConnection(constr); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "update_department"; 
    cmd.Parameters.Add("@DeptId", SqlDbType.Int).Value = Convert.ToInt32(e.Keys["DeptId"]); 
    cmd.Parameters.Add("@DeptName", SqlDbType.NVarChar).Value = e.NewValues["DeptName"]; 

    cmd.Connection = con; 

    try 
    { 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     e.Cancel = true; 
     GridView1.CancelEdit(); 
     GridView1.DataBind(); 


    } 

    catch (Exception ex) 
    { 
     throw ex; 
    } 

    finally 
    { 
     con.Close(); 
     con.Dispose(); 
    } 

} 


protected void gridView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) 
{ 
    if (e.Parameters == "Reload") 
    BindDeapartment(); 
} 

答えて

0

私はあなたが条件ならば内部に入るとBindDeapartment()を実行していないプログラムがgridView_CustomCallbackに行く更新したがいないときだと思います。

だから、あなたは2つのオプションがあります。

ONE

gridView_CustomCallbackからあれば削除します。

protected void gridView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) 
     {   
      BindDeapartment(); 
} 

TWO

Gridview1_RowUpdatingファイルの最後の行にBindDepartment()を追加および削除GridView1.DataBind();今neccesaryではないので:あなたは部門のビジネスクラスを作成することができます

protected void Gridview1_RowUpdating(object sender, ASPxDataUpdatingEventArgs e) 
    { 

SqlConnection con = new SqlConnection(constr); 
SqlCommand cmd = new SqlCommand(); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.CommandText = "update_department"; 
cmd.Parameters.Add("@DeptId", SqlDbType.Int).Value = Convert.ToInt32(e.Keys["DeptId"]); 
cmd.Parameters.Add("@DeptName", SqlDbType.NVarChar).Value = e.NewValues["DeptName"]; 

cmd.Connection = con; 

try 
{ 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    e.Cancel = true; 
    GridView1.CancelEdit(); 
    BindDeapartment(); 


} 

catch (Exception ex) 
{ 
    throw ex; 
} 

finally 
{ 
    con.Close(); 
    con.Dispose(); 
} 

} 
0

、あなたがグリッドに結果セットを直接結合しているとして、上何度も何度も&バインドを取得するために(時のパフォーマンス)は非常に複雑すぎますすべての更新。

GridView1.DataSource = cmd.ExecuteReader(); 

あなたはだからのためのクラスを作成し、部署のオブジェクトにすべてのデータを割り当て、この

Department objDepartment = cmd.ExecuteReader(); 
    GridView1.DataSource = objDepartment; 

のように行うことができます。その後、そのオブジェクトをグリッドのデータソースとして設定します。

レコードを更新する場合は、データベース内の値を更新し、更新されたオブジェクトでグリッドを再バインドするときにオブジェクトも更新します。これにより、複数のデータベースヒットが減少します。

関連する問題