簡體   English   中英

如何在C#中將圖像列表分配給image屬性

[英]how to assign list of images to image attribute in c#

我有圖像列表,並希望將它們分配給圖像屬性以顯示在數據表中。 我怎樣才能做到這一點?
我的想法是我想從數據庫中檢索值,並依賴於我要顯示圖像的值。 例如; 如果值= 2,則顯示2張圖像; 如果= 3,則顯示3張圖像...

我得到了這一點:無法將System.Collections.Generic.List類型隱式轉換為System.Windows.Controls.Image的語句:Project.team_members = HandCards;

這是我的代碼:

項目類別

public class Projects
    {
        public string developer_name { get; set; }
        public  Image team_members { get; set; }
    }

ASMX

List<string> developerNames = new List<string>();
List<Image> HandCards = new List<Image>();

Image image = new Image();
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri("images/img.jpg", UriKind.Relative);
source.EndInit();
image.Source = source;

while (somthing)
{
  developerNames.Add((string)rdrDev["user_name"]);//value returned from DB
  HandCards.Add(image);
}

Project.developer_name = string.Join(", ", developerNames);//I want to do 
//something like this for the images. this statement put all developers 
//together seperated by ','; I want to do the same to the next statement but 
//don't know how to do it for image type
Project.team_members = HandCards;

ASPX

var datatableVariable = $('#projects-progress').DataTable({
  data: data,
  columns: [
         { 'data': 'developer_name' },
         { 'data': 'team_member' },
]

我已經有一段時間沒有使用過ASP了,但是對於您的問題的第一部分,您可以遵循與以下所示類似的實現:

在HTML標記/ ASPX頁面中:說您有三個圖像,即。

<img src="~/Img1.jpg" runat="server" />
<img src="~/Img2.jpg" runat="server" />
<img src="~/Img3.jpg" runat="server" />

在后面的C#代碼中:

string filePath1 = "Img1.jpg"; //You'll do the same for the other two images
Image img1 = new Image(string filePath1);
Image img2 = new Image(string filePath2);
Image img3 = new Image(string filePath3);

List<Image> listOfImageCollection = new List<Image>();
listOfImageCollection.Add(img1);
listOfImageCollection.Add(img2);
listOfImageCollection.Add(img3);

DataTable dt = new DataTable();

foreach(Image imgs in listOfImageCollection)
{
    dt.DataSource = imgs;
    dt.DataBind();
}

string myConnectionString = "";
SqlConnection mySQLConnection;
SqlCommand mySQLCommand;
SqlDataReader mySQLDataReader;



myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringToTheDB"].ConnectionString;
                using (mySQLConnection = new SqlConnection(myConnectionString))
                {
                    mySQLCommand = new SqlCommand("storedProcedureNameToGetValue", mySQLConnection);
                    mySQLCommand.CommandType = CommandType.StoredProcedure;
                    mySqlParameter = new SqlParameter();
                    mySqlParameter.ParameterName = "@Value";
                    mySqlParameter.Value = Value;
                    mySQLCommand.Parameters.Add(mySqlParameter);
                    mySQLConnection.Open();
                    mySQLDataReader = mySQLCommand.ExecuteReader();

                    //Create a class to hold your DB properties
                    ClassName classInstance = new ClassName();
                    while (mySQLDataReader.Read())
                    {
                        classInstance.Value = Convert.ToInt32(mySQLDataReader["DBColumnNameHoldingValue"]);                                           
                    }

                if(classInstance.Value == 1 && dt.Count == 1)
                {
                  //Display one Image then follow the same procedure for the other conditions
                }
             }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM