简体   繁体   English

根据数据库中的记录数显示许多usercontrol

[英]Display a number of usercontrol based on the number of records in the database

有效的XHTML . The black box is a usercontrol. 黑框是一个用户控件。

Usercontrol Code 用户控制码

 string b = "";
    public string ID
    {
        set { b = value; }
    }

    public void sample()
    {
        textBox1.Clear();
        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee where ID = @a", myDatabaseConnection))
            {
                SqlCommand.Parameters.AddWithValue("@a", b);
                DataSet DS = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
                da.Fill(DS, "Images");
                var imagesTable = DS.Tables["Images"];
                var imagesRows = imagesTable.Rows;
                var count = imagesRows.Count;

                if (count <= 0)
                    return;
                var imageColumnValue =
                    imagesRows[count - 1]["Image"];
                if (imageColumnValue == DBNull.Value)
                    return;

                var data = (Byte[])imageColumnValue;
                using (var stream = new MemoryStream(data))
                {
                    pictureBox1.Image = Image.FromStream(stream);
                }

            }
        }   
    }

    public void getname()
    {
        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LastName from Employee where ID =  @a", myDatabaseConnection))
            using (SqlDataAdapter da = new SqlDataAdapter(SqlCommand))
            {
                SqlCommand.Parameters.AddWithValue("@a", b);

                SqlDataReader DR1 = SqlCommand.ExecuteReader();
                if (DR1.Read())
                {
                    textBox1.Text = DR1.GetString(DR1.GetOrdinal("LastName")).ToString();
                }
            }
        }
    }

Form Code 表格代码

public string ID
    {
        get { return textBox1.Text; }
    }

        private void textBox1_TextChanged(object sender, EventArgs e)
    {
        userControl21.ID = ID;
        userControl21.sample();
        userControl21.getname();
    }

Using the code above I can display a single record to the usercontrol. 使用上面的代码,我可以向用户控件显示一条记录。 How i can display a number of usercontrol based on the number of records in the database with their picture and name? 我如何基于数据库中记录的数量及其图片和名称显示许多usercontrol? For example I have 7 records in the database, the form will display 7 usercontrols. 例如,我在数据库中有7条记录,该表单将显示7个用户控件。 And when I click or select a usercontrol more information such as Address, Contacts, Etc will be display in my form. 当我单击或选择一个用户控件时,更多信息(例如地址,联系人,等等)将显示在我的表单中。 If the records in the database is too many to fit in the form there will be a vertical or horizontal scroll bar. 如果数据库中的记录太多而无法容纳表格,则将出现垂直或水平滚动条。 Give me some code :) 给我一些代码:)

What is the best container to display the usercontols that can have scroll bar? 显示具有滚动条的用户对话框的最佳容器是什么? a panel, groupbox or anything? 面板,组框或其他内容?

This must be the sample output. 这必须是样本输出。 有效的XHTML .

In my opinion the best container in your case will be FlowLayoutPanel with its FlowDirection property set to FlowDirection.LeftToRight . 在我看来,最好的容器是FlowLayoutPanel ,其FlowDirection属性设置为FlowDirection.LeftToRight It will be especially usefull since all your custom user controls have the same size (from what I can see in your second screenshot). 由于所有自定义用户控件的大小都相同(从第二张屏幕截图中可以看到),这将特别有用。 And about your first question: you could create your own descendant of FlowLayoutPanel and add some method like LoadAllItemsFromDataBase where you could create your custom user control for every record like you do now and add it dinamically to Controls collection of FlowLayoutPanel 关于第一个问题:您可以创建自己的FlowLayoutPanel子代,并添加诸如LoadAllItemsFromDataBase之类的方法,您可以像现在一样为每条记录创建自定义用户控件,然后将其动态添加到FlowLayoutPanel Controls集合中。

We have created a view very much like what you are describing. 我们已经创建了一个非常类似于您所描述的视图。 We have a Container class that is inherited from Form and is custom drawn (it's actually an abstract class that we further inherit from in order to create many types of containers that all share common functionality). 我们有一个从Form继承并自定义绘制的Container类(它实际上是一个abstract类,我们进一步继承它以创建许多类型的,具有相同功能的容器)。 We add these Containers dynamically to a Panel control based on having looped through records in a database to retrieve information about how the Containers are to be constructed. 我们基于遍历数据库中的记录来动态地将这些Containers添加到Panel控件中,以检索有关如何构造Containers信息。 Being its own class means that you can have custom handlers for any event you choose, such as reacting to user selection (to fetch more information like the address and contacts). 作为自己的类,意味着您可以为选择的任何事件提供自定义处理程序,例如对用户选择做出反应(以获取更多信息,例如地址和联系方式)。

In your example, I don't see a class for what you call "UserControl". 在您的示例中,我没有看到所谓的“ UserControl”类。 I would suggesting starting there. 我建议从那里开始。 A concrete class defining a "container" object is what will anchor this project. 定义“容器”对象的具体类将锚定该项目。

In your last screenshot, the red outline should be a Panel control. 在上一个屏幕截图中,红色轮廓应为Panel控件。 Panel will allow scrolling. Panel将允许滚动。 Alternatively, you could also use a FlowLayoutControl if you would prefer that the control that owns your containers be in control of positioning and spacing. 另外,如果您希望拥有容器的控件可以控制位置和间距,也可以使用FlowLayoutControl We went with a Panel to retain control over absolute positioning. 我们与一个Panel去保持对绝对定位的控制。

Once you have a container class and a form with a Panel on it, you can loop through the records you retrieve from your database, adding a container object for each one, and setting its values simultaneously (based on the example, some text and an image). 有了容器类和带有Panel的窗体之后,就可以遍历从数据库中检索的记录,为每个容器添加一个容器对象,并同时设置其值(基于示例,一些文本和一个图片)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM