动态文本框

时间:2014-03-30 18:48:36

标签: c# textbox

我正在从我的数据库中访问记录..并生成动态文本框以获取记录数量。

然后我从数据集中为这些文本框分配文本..

我在组合框的Change事件中执行此操作..但是当我更改事件时..这是组合框中的更改值,文本框中的值仍然保持相同,即获取从以前的事件......

文本框中的值不会更新。

这是我的代码

private void ComBoSem_SelectedIndexChanged(object sender, EventArgs e)
{
    semID();
    conct.MyConnection();

    try
    {
        SqlCommand SubjectsCMD = new SqlCommand("select SubjectName from Subjects where Sem_ID= '" + semesterID + "'", Connectioncs.con);
        SqlDataAdapter SubjectDA = new SqlDataAdapter();
        conct.openCon();
        SubjectDA.SelectCommand = SubjectsCMD;

        SubjectsCMD.ExecuteNonQuery();

        subjectDS.Clear();

        SubjectDA.Fill(subjectDS, "sub");

        if (subjectDS.Tables["sub"].Rows.Count > 0)
        {
            TextBox[] txtsubject = new TextBox[subjectDS.Tables["sub"].Rows.Count];

            DateTimePicker[] DtPker = new DateTimePicker[subjectDS.Tables["sub"].Rows.Count];

            Label lblSubjectName = new Label();
            lblSubjectName.Text = "Subjects Name";
            lblSubjectName.Location = new Point(60, 115);
            GB_DateSHeet.Controls.Add(lblSubjectName);

            Label lblPaperTime = new Label();
            lblPaperTime.Text = "Subjects Name";
            lblPaperTime.Location = new Point(300, 115);
            GB_DateSHeet.Controls.Add(lblPaperTime);

            ////// label Control Finished here 

            for (int i = 0; i < subjectDS.Tables["sub"].Rows.Count; i++)
            {
                //// dynamic Textboxes 
                txtsubject[i] = new TextBox();
                Point p = new Point(50, 143 + (i * 28));
                txtsubject[i].Size = new Size(168, 21);
                txtsubject[i].Location = p;

                DtPker[i] = new DateTimePicker();
                DtPker[i].Location = new Point(300, 143 + (i * 28));

                txtsubject[i].Text = subjectDS.Tables["sub"].Rows[i]["SubjectName"].ToString();
            }

            conct.openCon(); 

            // adding controls to GridView 
            for (int i = 0; i < subjectDS.Tables["sub"].Rows.Count; i++)
            {
                GB_DateSHeet.Controls.Add(txtsubject[i]);

                GB_DateSHeet.Controls.Add(DtPker[i]);
            }
        }
        else 
        {
            MessageBox.Show("No Record  Found");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

0 个答案:

没有答案