DevExpress在更新问题

时间:2017-06-29 19:16:57

标签: c# asp.net devexpress aspxgridview

嗨,我收到一些关于ASPxGridview的问题。下面是我的代码,我可以成功地将记录更新回数据库。但是当我点击“更新”编辑表格数据保存成功但aspxgridview不会自动刷新,直到我按F5或页面重新加载按钮。如何在不重新加载的情况下反映这些更改。我可以点击“EditFormUpdateButton”调用javascript函数。请帮我解决这个问题。

[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();
}

2 个答案:

答案 0 :(得分:0)

我认为当您更新程序时,请转到gridView_CustomCallback,但如果条件未执行BindDeapartment()则不会进入内部。

所以你有两个选择:

<强> ONE

gridView_CustomCallback删除

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

<强> TWO

BindDepartment()添加到Gridview1_RowUpdating文件的最后一行并删除GridView1.DataBind();,因为现在不需要:

 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();
}

}

答案 1 :(得分:0)

您可以为Department创建业务类,并且当您将结果集直接绑定到网格时,要获取和获取它是非常复杂的(时间性能)。每次更新时都会反复绑定。

GridView1.DataSource = cmd.ExecuteReader();

你可以这样做

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

所以为它创建一个类并将所有数据分配给Department的对象。然后将该对象设置为网格的数据源。

对于更新任何记录的情况,当您要更新数据库中的值并使用更新的对象重新绑定网格时,也要更新该对象。这将减少多个数据库命中。