如何以正确的顺序将OLEDB DataReader值添加到TableLayoutPanel?

时间:2018-12-02 07:28:33

标签: c# ms-access tablelayoutpanel oledbdatareader

我想将数据库表值添加到TableLayoutPanel控件中。我想出了使用OLEDB DataReader的解决方案,该解决方案将我的数据库值带入TableLayoutPanel。但是它的顺序不正确。我想要的是将我的数据库表列名称(在我的情况下为reader.GetName(index);)始终添加到TablelayoutPanel和Datareader行的第一行(在我的情况下为reader.GetString(index).Trim ();)转换为正确的顺序。但是,当我执行以下代码时,会如下图所示显示我的表格布局面板控件

This is the ScreenShot of my TableLayoutPanel

            string queryString = "SELECT * FROM Student";
        try
        {

                OleDbCommand command = new OleDbCommand(queryString, conn);
                conn.Open();
                OleDbDataReader reader = command.ExecuteReader();
            while (reader.HasRows)
            {
                FeeDetailTAble.ColumnCount = 7;
                FeeDetailTAble.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 40F));
                FeeDetailTAble.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 30F));
                FeeDetailTAble.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 30F));
                FeeDetailTAble.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 40F));
                FeeDetailTAble.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 30F));
                FeeDetailTAble.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 30F));
                FeeDetailTAble.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize, 30F));

                FeeDetailTAble.RowCount = FeeDetailTAble.RowCount + 1;
                FeeDetailTAble.RowStyles.Add(new RowStyle(SizeType.Percent, 20F));
                FeeDetailTAble.Controls.Add(new Label() { Text = reader.GetName(0) }, 0, 0);
                FeeDetailTAble.Controls.Add(new Label() { Text = reader.GetName(1) }, 1, 0);
                FeeDetailTAble.Controls.Add(new Label() { Text = reader.GetName(2) }, 2, 0);
                FeeDetailTAble.Controls.Add(new Label() { Text = reader.GetName(3) }, 3, 0);
                FeeDetailTAble.Controls.Add(new Label() { Text = reader.GetName(4) }, 4, 0);
                FeeDetailTAble.Controls.Add(new Label() { Text = reader.GetName(5) }, 5, 0);
                FeeDetailTAble.Controls.Add(new Label() { Text = reader.GetName(6) }, 6, 0);

                while (reader.Read())
                {
                    string Name = reader.GetString(0).Trim();
                    string FatherName = reader.GetString(1).Trim();
                    string Class = reader.GetString(2).Trim();
                    string Date = reader.GetString(3).Trim();
                    string Fee = reader.GetString(4).Trim();
                    string Contact = reader.GetString(5).Trim();
                    string Email = reader.GetString(6).Trim();

                    FeeDetailTAble.RowCount = FeeDetailTAble.RowCount + 1;
                    FeeDetailTAble.RowStyles.Add(new RowStyle(SizeType.Absolute, 20F));

                    FeeDetailTAble.Controls.Add(new Label() { Text = Name }, 0, 0);
                    FeeDetailTAble.Controls.Add(new Label() { Text = FatherName }, 1, 0);
                    FeeDetailTAble.Controls.Add(new Label() { Text = Class }, 2, 0);
                    FeeDetailTAble.Controls.Add(new Label() { Text = Date }, 3, 0);
                    FeeDetailTAble.Controls.Add(new Label() { Text = Fee }, 4, 0);
                    FeeDetailTAble.Controls.Add(new Label() { Text = Contact }, 5, 0);
                    FeeDetailTAble.Controls.Add(new Label() { Text = Email }, 6, 0);
                }
                reader.NextResult();
            }
            reader.Close();

            conn.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

我希望我的问题有效且具体

0 个答案:

没有答案