单击事件刷新GridView

时间:2016-03-03 10:46:02

标签: c# asp.net devexpress

目前,我正在开发一个网络表单应用,并在DevExpress的所选值上填充ASPxComboBox网格。我有这个部分没问题。但是,如果有任何新数据添加到数据库中,我的要求是刷新网格,以便用户可以看到这一点。所以我添加了一个按钮,在click事件中我尝试刷新数据,但网格没有刷新。如果我单步执行代码,我可以看到数据库中的新值存在于DataTable中。我似乎无法弄清楚什么可能导致网格不刷新。任何帮助将受到高度赞赏。

这是我到目前为止所拥有的

SelectedIndexChangedEvent

protected void TrainingOptions_SelectedIndexChanged(object sender, EventArgs e)
{
   ASPxComboBox ddl = (ASPxComboBox)sender;
   string[] parameters = { ddl.SelectedItem.Value.ToString() };
   TrainingGrid.DataSource = dto.PopulateTrainingData(parameters);
   TrainingGrid.DataBind();
}

ButtonClickEvent

protected void Refresh_Click(object sender, EventArgs e)
{
   string[] parameters = { TrainingOptions.SelectedItem.Value.ToString() };
   TrainingGrid.DataSource = dto.PopulateTrainingData(parameters);
   TrainingGrid.DataBind();
 }

PopulateTrainingDara

public DataTable PopulateTrainingData(params string[] parameters)
{
   //Loop through DataTable Here
   ...

   HttpContext.Current.Session["GridDT"] = mainTable;
   return mainTable;
}

数据绑定

protected void TrainingGrid_DataBinding(object sender, EventArgs e)
{
   TrainingGrid.DataSource = Session["GridDT"];
}

Page_LoadEvent

if (!IsPostBack)
{
    DataTable dropDownOptions = dto.GetTrainingData("MyQuery");

    //Add datasource to TrainingOptions dropdown
    TrainingOptions.DataSource = dropDownOptions;
    TrainingOptions.Text = "Please choose";
    TrainingOptions.TextField = "ID";
    TrainingOptions.DataBindItems();
}

标记

<dx:ASPxComboBox ID="TrainingOptions" runat="server" ValueType="System.String" OnSelectedIndexChanged="TrainingOptions_SelectedIndexChanged" CssClass="combo-box"></dx:ASPxComboBox>
<dx:ASPxGridView ID="TrainingGrid" runat="server" OnHtmlDataCellPrepared="TrainingGrid_HtmlDataCellPrepared" OnDataBinding="TrainingGrid_DataBinding"></dx:ASPxGridView>
<dx:ASPxButton ID="Refresh" runat="server" Text="Refresh" OnClick="Refresh_Click" AutoPostBack="false"></dx:ASPxButton>

注意:我没有在Page_Load事件中填充网格。

如果需要任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

好的,我已经使用DevExpress Suite的最新试用版重新创建了您的示例。我删除了不必要的代码,这是我的情况下的代码:

public partial class DevExpress : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dropDownOptions = new DataTable();
            dropDownOptions.Columns.Add("id");
            DataRow row = dropDownOptions.NewRow();
            row["id"] = 1;
            dropDownOptions.Rows.Add(row);
            row = dropDownOptions.NewRow();
            row["id"] = 2;
            dropDownOptions.Rows.Add(row);
            dropDownOptions.AcceptChanges();

            TrainingOptions.DataSource = dropDownOptions;
            TrainingOptions.Text = "Please choose";
            TrainingOptions.TextField = "ID";
            TrainingOptions.DataBindItems();
        }
    }

    protected void Refresh_Click(object sender, EventArgs e)
    {
        string[] parameters = { TrainingOptions.SelectedItem.Value.ToString() };
        TrainingGrid.DataSource = PopulateTrainingData(parameters);
        TrainingGrid.DataBind();
    }

    protected void TrainingOptions_SelectedIndexChanged(object sender, EventArgs e)
    {
        ASPxComboBox ddl = (ASPxComboBox)sender;
        string[] parameters = { ddl.SelectedItem.Value.ToString() };
        TrainingGrid.DataSource = PopulateTrainingData(parameters);
        TrainingGrid.DataBind();
    }

    public DataTable PopulateTrainingData(params string[] parameters)
    {
        DataTable mainTable = (DataTable)Session["GridDT"] ?? new DataTable();
        if (!mainTable.Columns.Contains("id"))
        {
            mainTable.Columns.Add("id");
        }

        DataRow row = mainTable.NewRow();
        row["id"] = parameters[0];
        mainTable.Rows.Add(row);
        mainTable.AcceptChanges();

        HttpContext.Current.Session["GridDT"] = mainTable;
        return mainTable;
    }
}

和aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="DevExpress.aspx.cs" Inherits="WebApplication1.DevExpress" %>

<%@ Register Assembly="DevExpress.Web.v15.2, Version=15.2.7.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <dx:ASPxComboBox ID="TrainingOptions" runat="server" ValueType="System.String" OnSelectedIndexChanged="TrainingOptions_SelectedIndexChanged" AutoPostBack="true" CssClass="combo-box"></dx:ASPxComboBox>
    <dx:ASPxGridView ID="TrainingGrid" runat="server"></dx:ASPxGridView>
    <dx:ASPxButton ID="Refresh" runat="server" Text="Refresh" OnClick="Refresh_Click"></dx:ASPxButton>
</asp:Content>

如果这对您不起作用,您应该检查您使用的DevExpress控件的版本,如果可能,可以更新它们。或者也许你正在做一些你没有在这里发布的事情。