显示基于记录在数据库中的数的数的usercontrol的 [英] Display a number of usercontrol based on the number of records in the database

查看:121
本文介绍了显示基于记录在数据库中的数的数的usercontrol的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


中的黑盒子是一个用户控件。



用户控件代码



 字符串b =; 
公共字符串ID
{
集合{B =价值; }
}

公共无效样本()
{
textBox1.Clear();
使用(SqlConnection的myDatabaseConnection =新的SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();使用
(的SqlCommand的SqlCommand =新的SqlCommand(选择姓氏,从员工那里图片ID = @a,myDatabaseConnection))
{
SqlCommand.Parameters.AddWithValue(@一,B );
的DataSet DS =新的DataSet();
SqlDataAdapter的大=新SqlDataAdapter的(的SqlCommand);
da.Fill(DS,图像);
VAR imagesTable = DS.Tables [图像];
VAR imagesRows = imagesTable.Rows;
变种数= imagesRows.Count;

如果(计数< = 0)
的回报;
变种imageColumnValue =
imagesRows [数 - 1] [图像];
如果(imageColumnValue == DBNull.Value)
的回报;

VAR数据=(字节[])imageColumnValue;
使用(VAR流=新的MemoryStream(数据))
{
pictureBox1.Image = Image.FromStream(流);
}

}
}
}

公共无效的getName()
{使用
(SqlConnection的myDatabaseConnection =新的SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();使用(的SqlCommand的SqlCommand =新的SqlCommand(从员工选择姓氏,其中ID = @a,myDatabaseConnection))使用
(SqlDataAdapter的大=新SqlDataAdapter的(的SqlCommand))
{$ B $
b SqlCommand.Parameters.AddWithValue(@一,b);

SqlDataReader的DR1 = SqlCommand.ExecuteReader();
如果(DR1.Read())
{
textBox1.Text = DR1.GetString(DR1.GetOrdinal(名字))的ToString()。
}
}
}
}



表代码

 公共字符串ID 
{
{返回textBox1.Text; }
}

私人无效textBox1_TextChanged(对象发件人,EventArgs五)
{
userControl21.ID = ID;
userControl21.sample();
userControl21.getname();
}



使用上面的代码,我可以显示一个记录到该用户控件。我怎么能显示基于记录在他们的照片和名字的数据库数量的一些UserControl的?例如,我在数据库中有7条记录,表单将显示用户控件7。当我点击或选择一个用户控件的详细信息,如地址,联系人等将在我的形式显示。如果在数据库中的记录是太多,以适应的形式会有一个垂直或水平滚动条。给我一些代码:)



什么是显示可以有滚动条usercontols最好的容器?面板,组框或什么?



这必须是样本输出。


< DIV CLASS =h2_lin>解决方案

在我看来你的情况最好的容器是 FlowLayoutPanel的的FlowDirection 属性设置为 FlowDirection.LeftToRight 。这将是特别有用的,因为所有的自定义用户控件具有相同的尺寸(从我可以在你的第二个屏幕看到的)。而关于你的第一个问题:你可以创建自己的后代 FlowLayoutPanel的并添加像LoadAllItemsFromDataBase一些方法,在那里你可以创建自定义的用户控件的每一条记录像你现在要做的,并添加它dinamically到控制 FlowLayoutPanel的


的集合

. 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? For example I have 7 records in the database, the form will display 7 usercontrols. 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. .

解决方案

In my opinion the best container in your case will be FlowLayoutPanel with its FlowDirectionproperty set to 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

这篇关于显示基于记录在数据库中的数的数的usercontrol的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆