【问题标题】:Display pictures with their name from database in winforms?在winforms中显示带有数据库名称的图片?
【发布时间】:2013-05-22 15:59:54
【问题描述】:
dataGridView1.DataSource = null;

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
    myDatabaseConnection.Open();
    using (SqlCommand mySqlCommand = new SqlCommand("Select Name, Picture from Employee ", myDatabaseConnection))
    {
        DataSet ds = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(mySqlCommand);
        adapter.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];  
    }
}

我如何在面板或数据库中的其他控件中显示带有名称的图片,而不是使用 datagridview,例如这种格式 http://static.neatorama.com/images/2008-04/yearbook-project-robot-johnny.gif 以 win 形式显示? 例如我在数据库中有 7 条记录,表单将显示 7 个带有图片和标签的面板。当我单击或选择一个面板时,它将在文本框中显示更多信息,例如地址、联系人等。如果数据库中的记录太多而无法放入表单中,则会出现垂直或水平滚动条。

【问题讨论】:

  • 您可以动态创建设计。您是否查找内置控件?
  • 你可以使用中继器。
  • @Furkan - 不,先生。例如我在数据库中有 7 条记录,将显示 7 个带有标签的图片框。如果记录太多而无法放入表格中,则会有一个滚动条。

标签: c# sql-server database winforms photo


【解决方案1】:

我建议你动态创建你需要的对象。您可以使用“位置”来定位您的对象。

例子:

int cptx = 0;
int cpty = 0;

for(int i=0; i<ds.Tables[0].Rows.Count;i++)
{
    PictureBox currentpic = new PictureBox();
    Label currentlabel = new Label();

    currentpic.Size = new Size(20,20);
    currentlabel.Size = new Size(20,20);

    currentpic.BorderStyle = BorderStyle.FixedSingle;
    currentlabel.Text = "te";

    if(cptx >= 4)
    {
        cptx = 0;
        cpty ++;
    }

    currentpic.Location= new Point((cptx*25),(cpty*50));
    currentlabel.Location = new Point((cptx*25),(cpty*50)+25);

    This.Controls.Add(currentpic);

    cptx ++;

}

这段代码应该这样做:

编辑:我建议你看看“用户控件”,用户控件允许你创建自己的自己的控件.因此,图片和标签将位于单个控件中。使用用户控件的好处是您将创建的控件将完全可重复用于您的其他窗口和/或程序。

【讨论】:

  • 所以它会像这样 currentlabel = sqlreader.GetString(sqlreader.GetOrdinal("Name"));
  • 或者你可以这样做:currentlabel.Text = ds.Tables[0].Rows[i]["Name"].toString();
  • 你能给我举个用户控制的例子吗?在这种情况下我将如何使用它?谢谢你:)
  • 是的,但您必须先了解什么是“用户控件”,然后才能使用它。我建议你阅读它。 Tuto
  • 你能给我一个很好的链接来阅读用户控制吗?谢谢你:)
【解决方案2】:

Form只有一个面板(pnlGrid)并且面板的AutoScroll属性设置为true;

DataTable dtImage = new DataTable();

dtImage.Columns.Add("Path");
dtImage.Columns.Add("Name");

dtImage.Rows.Add(new object[] { "ImagePath", "ImageText" });
dtImage.Rows.Add(new object[] { "ImagePath", "ImageText" });
dtImage.Rows.Add(new object[] { "ImagePath", "ImageText" });

CreateImageGrid(dtImage);

和方法;

private void CreateImageGrid(DataTable dtDataSource)
{
    int colCount = 5;
    int rowCount = 0;
    int imgWidth = 100;
    int imgHeight = 100;
    int imgPadding = 10;
    int lblPadding = 5;
    int ind = -1;

    PictureBox pic = null;
    Label lbl = null;

    if (dtDataSource.Rows.Count > colCount)
    {
        rowCount = Convert.ToInt32(dtDataSource.Rows.Count / colCount);

        if (Convert.ToInt32(dtDataSource.Rows.Count % colCount) > 0)
        {
            rowCount++;
        }
    }
    else
    {
        rowCount = 1;
    }

    for (int j = 0; j < rowCount; j++)
    {
        for (int i = 0; i < colCount && dtDataSource.Rows.Count > ((j * colCount) + i); i++)
        {
            ind = (j * colCount) + i;

            pic = new PictureBox();
            pic.Image = Image.FromFile(dtDataSource.Rows[ind]["Path"].ToString());

            pnlGrid.Controls.Add(pic);

            pic.Width = imgWidth;
            pic.Height = imgHeight;
            pic.Top = (j * (imgHeight + imgPadding)) + imgPadding;
            pic.Left = (i * (imgWidth + imgPadding)) + imgPadding;

            lbl = new Label();
            lbl.Text = dtDataSource.Rows[ind]["Name"].ToString();

            pnlGrid.Controls.Add(lbl);

            lbl.Left = pic.Left;
            lbl.Top = pic.Top + pic.Height + lblPadding;
        }
    }
}

注意:文本长度可能会导致问题,你应该定义一些规则。

【讨论】:

    猜你喜欢
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2020-05-11
    • 2014-09-21
    • 2014-06-10
    相关资源
    最近更新 更多