根据下拉列表更改GridView的数据源

时间:2019-02-14 14:05:52

标签: c# asp.net gridview

我很努力,希望有人能帮助我。

我有4个下拉列表,一个搜索按钮和一个带有基本SQLdataConnection的普通gridview。

SQLdataConnection是一个存储过程,它基于4个参数检索3个表。然后将这3个表连接到gridview中。

但是

其中一个下拉列表具有总计,快速选项。因此,当用户按下搜索按钮时,应根据用户选择的选项将相应的表加载到gridview中。 (因此,如果选择了total,则应该将total table加载到gridview中)

我不知道是否应使用每个选项更改SQLdataConnection以仅显示适当的表。

下面是下拉列表和gridview的html以及后面的代码的副本。

<tr>
  <td style="text-align: right" class="pad-right-0">
    <asp:DropDownList ID="selSeconds" runat="server" CssClass="selectbox select-chr" Visible="true"></asp:DropDownList>
    <asp:DropDownList ID="selHours" runat="server" CssClass="selectbox select-chr" Visible="true"></asp:DropDownList>
    <asp:DropDownList ID="selMerchantId" runat="server" CssClass="selectbox select-chr" Visible="true"></asp:DropDownList>
    <asp:DropDownList ID="selTableType" runat="server" CssClass="selectbox select-chr" Visible="true"></asp:DropDownList>
    <asp:Button ID="btnSearch" runat="server" CssClass="btn btn-primary btn-danger tip-s medium grey" ValidationGroup="Search" OnClick="btnSearch_Click" Text="<%$Resources:Resource, btnSearch %>" />
    <asp:ImageButton ID="btnExportToExcel" runat="server" CssClass="btn btn-primary btn-danger tip-s medium grey" ValidationGroup="Search" onclick="btnExportToExcel_Click" Text="Export to excel" ImageUrl="images/Excel.png" AlternateText="<%$Resources:Resource, lblExportExcel %>"
      CausesValidation="false" />
    <asp:RequiredFieldValidator ID="rqrdMerchantId" runat="server" ErrorMessage="<%$Resources:Resource, lblRequired %>" ControlToValidate="selMerchantId" ForeColor="Red" />
  </td>
</tr>
</table>
<asp:HiddenField ID="hdnMerchantId" runat="server" Value="0" />
<asp:GridView ID="gridSpeedAnalysis" runat="server" DataSourceID="gridSqlConnection">
</asp:GridView>
<asp:SqlDataSource ID="gridSqlConnection" runat="server"></asp:SqlDataSource>

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null || Session["UserType"] == null)
        {
            Response.Redirect("Login.aspx");
        }

        LoadDropDowns();

    }



    private void LoadDropDowns()
    {
        System.Web.UI.WebControls.ListItem item;
        selSeconds.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select Second", "0"));
        int index = 1;
        for (int Second = 0; Second <= 10; Second++)
        {
            System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem(Second.ToString(), Second.ToString());
            selSeconds.Items.Insert(index, li);
            index++;
        }

        item = selSeconds.Items.FindByValue(DateTime.Now.Year.ToString());
        selSeconds.SelectedIndex = selSeconds.Items.IndexOf(item);

        if (Session["UserType"] != null && (Session["UserType"].ToString().ToLower() == "System Administrator".ToLower()))
        {
            selMerchantId.DataSource = BOL.MerchantGroup.Load();
            selMerchantId.DataTextField = "Description";
            selMerchantId.DataValueField = "MerchantId";
            selMerchantId.DataBind();
            selMerchantId.Items.Insert(0, new System.Web.UI.WebControls.ListItem { Value = "0", Text = "Select Group" });
            rqrdMerchantId.InitialValue = "0";
        }
        else if (Session["UserType"].ToString().ToLower() == "Head Office".ToLower())
        {
            BOL.MerchantGroup group = new BOL.MerchantGroup(int.Parse(hdnMerchantId.Value));
            selMerchantId.Items.Insert(0, new System.Web.UI.WebControls.ListItem { Value = hdnMerchantId.Value, Text = group.Description });
        }

        DataTable dt = BOL.Merchant.GetDropdownByMerhcantGroupId(int.Parse(Session["MerchantGroupId"].ToString()));

        selMerchantId.DataSource = dt;
        selMerchantId.DataTextField = "Name";
        selMerchantId.DataValueField = "MerchantId";
        selMerchantId.DataBind();

        selTableType.Items.Insert(0, new System.Web.UI.WebControls.ListItem {Text = "Total"});
        selTableType.Items.Insert(0, new System.Web.UI.WebControls.ListItem {Text = "Fast" });
        selTableType.Items.Insert(0, new System.Web.UI.WebControls.ListItem {Text = "Slow" });

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {          

    }

0 个答案:

没有答案