我的复选框列表不显示来自数据库的多个数据

本文关键字:数据库 数据 复选框 列表 显示 我的 | 更新日期: 2023-09-27 18:14:35

我试图基于两个输入变量以编程方式绑定我的复选框列表;但我只得到一个复选框,而不是3个。

这是我的代码

这是我的业务层

 public class BALDisplayPanel2
{
    private string _mylabel;
    public string MyLabel
    {
        get { return _mylabel;  }
        set { _mylabel = value; }
    }
    private string _conditionlabel;
    public string ConditionLabel
    {
        get { return _conditionlabel; }
        set { _conditionlabel = value; }
    }

    private string _checkboxquestion;
    public string CheckBoxQuestion
    {
        get { return _checkboxquestion; }
        set { _checkboxquestion = value; }
    }

这是数据访问层

 public List<BALDisplayPanel2> DisplaySPanelQ(int tbid, int grdid)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["mynewdbConnectionString"].ConnectionString);
        conn.Open();
        SqlCommand cmd = new SqlCommand("esp_MyCheckboxProc", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        List<BALDisplayPanel2> lst = new List<BALDisplayPanel2>();
        cmd.Parameters.AddWithValue("@Emp", tbid);
        cmd.Parameters.AddWithValue("@UnitNumber", grdid);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            BALDisplayPanel2 unt = new BALDisplayPanel2();
            unt.CheckBoxQuestion = dr["CheckQuest"].ToString();
            unt.MyLabel = dr["MyLabel"].ToString();
            unt.ConditionLabel = dr["ConditionLabel"].ToString();
            //unt.LabelS = dr["LabelQ2"].ToString();
            lst.Add(unt);
        }
        conn.Close();
        return lst;
    }

这是我的Default.cs文件,我称之为我的复选框

        BALDisplayPanel2 bl = new BALDisplayPanel2();
        DALDisplayPanel2 dal = new DALDisplayPanel2();
        List<BALDisplayPanel2> lst = new List<BALDisplayPanel2>();
        lst = dal.DisplaySPanelQ(Convert.ToInt32(tbEmpID.Text), Convert.ToInt32(GridView1.SelectedRow.Cells[2].Text));
        foreach (var item in lst)
        {
            chbklstpanel3.Items.Clear();
            chbklstpanel3.DataSource = lst;
            chbklstpanel3.DataTextField = item.CheckBoxQuestion;
            lblpanel3.Text = item.MyLabel;
            lblCondition.Text = item.ConditionLabel;

            }

任何帮助感谢

我的复选框列表不显示来自数据库的多个数据

实际上你在这里做的是循环结果并在里面绑定checklistbox。如果你有三条记录,那么它会循环三次。您还提到了clear()循环中的复选框列表。因此,当它第三次出现时,它将清除复选框列表并仅绑定最后一条记录。这就是你得到的,在复选框列表中只有一条记录。

所以删除foreach循环,

    BALDisplayPanel2 bl = new BALDisplayPanel2();
    DALDisplayPanel2 dal = new DALDisplayPanel2();
    List<BALDisplayPanel2> lst = new List<BALDisplayPanel2>();
    lst = dal.DisplaySPanelQ(Convert.ToInt32(tbEmpID.Text), Convert.ToInt32(GridView1.SelectedRow.Cells[2].Text));

        chbklstpanel3.Items.Clear();
        chbklstpanel3.DataSource = lst;
        chbklstpanel3.DataTextField = item.CheckBoxQuestion;
        chbklstpanel3.DataBind();