将第一个值设置为 NULL 值到 DropDownMenu C#

Set the first value as NULL value to DropDownMenu C#

当我 运行 应用程序下拉菜单的值始终设置为 0 并在 Report 中显示结果时。

我想修改这些以在 DropDownMenu 中添加 text 并且当用户未 select 编辑任何内容时它应该 return 所有数据,如果用户 select 下拉列表中的值它应该 return 用户 selected.

的值

第一个下拉菜单

public void FillOrgUnit()
{
            using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True"))
            {
                string com = "SELECT DISTINCT OrgUnitID FROM tblZaposleni_AD ORDER BY OrgUnitID ASC";
                SqlDataAdapter adpt = new SqlDataAdapter(com, conn);
                DataTable dt = new DataTable();
                adpt.Fill(dt);
                ddlOrgUnit.DataSource = dt;
                ddlOrgUnit.DataTextField = "OrgUnitID";
                ddlOrgUnit.DataValueField = "OrgUnitID";
                ddlOrgUnit.DataBind();

                ddlOrgUnit.Items.Insert(0, new ListItem("-- Izaberi Org Jedinicu --", "NULL"));
            }
}

第二个下拉菜单:

public void FillStatus()
{
        using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True"))
        {
            string com = "SELECT DISTINCT Status FROM tblZaposleni_AD";
            SqlDataAdapter adpt = new SqlDataAdapter(com, conn);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            ddlStatus.DataSource = dt;
            ddlStatus.DataTextField = "Status";
            ddlStatus.DataValueField = "Status";
            ddlStatus.DataBind();

            ddlStatus.Items.Insert(0, new ListItem("-- Izaberi Status --", "NULL"));
        }
}

enter image description here

HTML

 <div>
            <p class="auto-style1">
                Izaberi Izvjestaj :
                <br class="auto-style1" />
                <asp:DropDownList ID="ddlReportName" runat="server" Width="168px" DataTextField="Value" DataValueField="Key" OnSelectedIndexChanged="ddlReportName_SelectedIndexChanged" Height="16px">
                </asp:DropDownList>
                <br class="auto-style1" />
                Org Unit
                <br class="auto-style1" />
                <asp:DropDownList ID="ddlOrgUnit" runat="server" Height="17px" OnSelectedIndexChanged="ddlOrgUnit_SelectedIndexChanged" Width="157px" AppendDataBoundItems="True">                
                <asp:ListItem Value="">-- Izaberi Org Jedinicu --</asp:ListItem>
                </asp:DropDownList>
                &nbsp;
                <br class="auto-style1" />
                Status:
                <br class="auto-style1" />
                <asp:DropDownList ID="ddlStatus" runat="server"  Height="16px" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged1" Width="147px" AppendDataBoundItems="True">               
                <asp:ListItem Value="">-- Izaberi Status --</asp:ListItem>
                </asp:DropDownList>
                &nbsp;
            </p>
            <p class="auto-style1">
                <br class="auto-style1" />
                <%--<a href="javascript:ShowReport()">Show</a>--%>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Filter" Width="224px" />

            </p>
        </div>

Page_Load 其中我调用 FillStatusFillOrgUnt metod

protected void Page_Load(object sender, EventArgs e)
{
            if (!IsPostBack)
            {
                string path = @"\Reports\";
                CustomReportStorageWebExtension reportsStorage = new CustomReportStorageWebExtension(path);
                ddlReportName.DataSource = reportsStorage.GetUrls();
                ddlReportName.DataBind();

                //Call function for populate cb
                FillStatus();
                FillOrgUnit();
            }
            else
            {
                XtraReport reportToOpen = null;

                switch (ddlReportName.SelectedValue)
                {
                    case "Zaposleni 1":
                        reportToOpen = new ZaposleniSaoOsig1();
                        break;

                    case "Zaposleni 2":
                        reportToOpen = new ZaposleniSaoOsig2();
                        break;

                    case "Zaposleni 3":
                        reportToOpen = new ZaposleniSaoOsig3();
                        break;
                }

                GetReports(reportToOpen);
                ASPxWebDocumentViewer1.OpenReport(reportToOpen);
            }
        }

过滤StatusOrgUnit

的主要功能
private void GetReports(XtraReport report)
{
            try
            {
                string connString = @"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True";
                SqlConnection conn = new SqlConnection(connString);
                string strproc = "TestReport";
                using (SqlDataAdapter sda = new SqlDataAdapter(strproc, connString))
                {
                    DataSet ds = new DataSet();
                    SqlCommand cmd = new SqlCommand();
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;

                    sda.SelectCommand.Parameters.Add("@Status", SqlDbType.Bit).Value = ddlStatus.SelectedValue == "1" ? true : false;
                    sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = ddlOrgUnit.SelectedValue;
                    sda.Fill(ds);

                    string[] arrvalues = new string[ds.Tables[0].Rows.Count];

                    for (int loopcounter = 0; loopcounter < ds.Tables[0].Rows.Count; loopcounter++)
                    {
                        //assign dataset values to array
                        arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["PrezimeIme"].ToString();
                        arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["NetworkLogin"].ToString();
                        arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["Status"].ToString();
                        arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["OrgUnitID"].ToString();
                    }


                    report.DataSource = ds;
                    report.DataMember = ds.Tables[0].TableName.ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

以及 return 过滤报告的存储过程 StatusOrgId

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[TestReport]
(           
    @Status bit,
    @OrgJed int
) 
AS  
BEGIN
    SELECT PrezimeIme, NetworkLogin, Status, OrgUnitId, DT_Creat, DT_Modif
    FROM [DesignSaoOsig1].[dbo].[tblZaposleni_AD]
    WHERE (@Status IS NULL OR Status = @Status) 
      AND (@OrgJed IS NULL OR OrgUnitID = @OrgJed)
END

您可以执行以下操作来绑定数据源:

    using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True"))
    {
      string com = "SELECT DISTINCT OrgUnitID FROM tblZaposleni_AD ORDER BY OrgUnitID ASC";    
      SqlDataAdapter adpt = new SqlDataAdapter(com, conn); 
      DataTable dt = new DataTable();    
      adpt.Fill(dt);    
      ddlOrgUnit.DataSource = dt;    
      ddlOrgUnit.DataBind();    
      ddlOrgUnit.DataTextField = "text field you want to bind";    
      ddlOrgUnit.DataValueField = "value field you want to bind";    
      ddlOrgUnit.DataBind();

      //add default value - you can then remove the default value from html
      ddlOrgUnit.Items.Insert(0, new ListItem("-- Izaberi Org Jedinicu --","N/A")
     }

上述逻辑也应该在 FillStatus() 方法中完成。

在您的 Page_Load 方法中执行以下操作

        if (!IsPostBack)
        {
           FillStatus();

           FillOrgUnit();
        }

在 ddlOrgUnit_SelectedIndexChanged 中 - 例如 - 您将相应地处理用户输入的值 select - 并过滤 select 值 - 或 return 全部.

注意 当您从查询中填充 DataTable(dt) 时 - 您将拥有来自以下 SQL table tblZaposleni_AD

的 table 结构

在这里 ddlOrgUnit.DataTextField = "text field you want to bind"; 您将添加要绑定的列名称作为文本文件 - 例如 Name

注意 如何在 C# 中使用 tryparse

    if (Int32.TryParse(ddlStatus.SelectedValue, out int theValue))
    {
        //is not null
        sda.SelectCommand.Parameters.Add("@OrgJed", SqlDbType.Int).Value = theValue
    }

    // is null and you dont pass the parameter 

然后在您的存储过程中将@OrgJed int 的默认值设置为 null

ALTER PROCEDURE [dbo].[TestReport]
(           
    @Status bit,
    @OrgJed int = NULL
) 
using(sqlconnection con=new sqlconnection(cs))
{
   sqlcommand cmd=new sqlcommand("select [datatextfield], [datavaluefield] from tbl",con);
   sqldatareader rdr=cmd.executereader();
   dropdown.datasource=rdr;
   dropdown.datatextfield=rdr[0];
   dropdown.datavaluefield=rdr[1];
   dropdown.databind();
}