1

Right now I am using the following code to populate my third column in DataGridView:

 Bitmap img = new   
 Bitmap(@"C:\Project\Images\Image1.jpg");
 DataGridView [2, 0].Value = img;           //2,0 is 1st row, 3rd column

 Bitmap img2 = new   
 Bitmap(@"C:\Project\Images\Image2.jpg");
 DataGridView [2, 1].Value = img2;           //2,1 is 2nd row, 3rd column

The third column is correctly populated with the image, but I have this code duplicated 26 more times. For simplicity sake I just want to know if their is a simpler way of doing this, such as making an array or inserting the images directly to my database. I apologize if this is a novice question, by I am relatively new to C#.

Thanks!

Drew B
  • 465
  • 3
  • 6
  • 18
  • You can avoid copy/paste using for loop for instance. – Hamlet Hakobyan Nov 19 '12 at 05:14
  • My images each have unique names and aren't actually imange1, image2... so I can't iterate a loop based on that. Would it be worth it for me just to change the names of all my images to be like that? – Drew B Nov 19 '12 at 05:33

4 Answers4

2

I would suggest using Directory.GetFiles(path); which gives you array of all files in directory. Using this, you don't need to worry about the name of the files to be image1,image2,..

string path = @"C:\Project\Images\";
            string[] arr =  Directory.GetFiles(path);
            for (int i = 0 ; i < arr.Length ; i++)
            {
                Bitmap img = new Bitmap(Path.Combine(path , arr[i]));
                DataGridView [2, i].Value = img;
            }
Amir
  • 9,577
  • 11
  • 41
  • 58
  • I did what you stated and added 'using System.IO;', but it said the given paths format is not supported. – Drew B Nov 19 '12 at 05:27
  • I've updated the code (notice the Path.Combine(path , arr[i])) http://stackoverflow.com/questions/7348768/the-given-paths-format-is-not-supported-c-sharp – Amir Nov 19 '12 at 05:42
  • Haha the first pic correctly matches up, then the next four are in the wrong places, then the rest are correct. I think this will do for now, excellent response and thanks for your help!!! – Drew B Nov 19 '12 at 05:46
1

something like:

int numberOfImages = 10; // Replace 10 with number of your images
for(int i=0; i<numberOfImages; i++)
{
   using(Bitmap img = new Bitmap(string.Format(@"C:\Project\Images\Image{0}.jpg", i+1)))
   {
       DataGridView [2, i].Value = img;
   }    
}
Dmytro
  • 16,668
  • 27
  • 80
  • 130
0

You need to learn concept of loops/iteration. Check articles on iteration statements like for and foreach.

 for (int i = 0; i < 26; i++)
 {
     DataGridView [i, 1].Value = 
        new Bitmap(String.Format(@"C:\Project\Images\Image{0}.jpg", i));
 }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

Try this

ASPX Gridview code

<asp:GridView ID="grdImages" runat="server" BorderStyle="None" GridLines="None"
        ShowHeader="false" AutoGenerateColumns="False" Width="240">
        <Columns>
            <asp:ImageField DataImageUrlField="FileName"></asp:ImageField>
        </Columns>
</asp:GridView>

cs code

 protected void Page_Load(object sender, EventArgs e)
    {
        string path = HttpContext.Current.Request.PhysicalApplicationPath + @"\Images\Temple";
        // string path = @"D:\Blog\ImageShow\Images"; // This statement also valid

        string[] extensions = { "*.jpg", "*.png", "*.bmp" };

        List<string> files = new List<string>();
        foreach (string filter in extensions)
        {
            files.AddRange(System.IO.Directory.GetFiles(path, filter));
        }

        IList<ImageFileInfo> imageFileList = new List<ImageFileInfo>();
        foreach (string strFileName in files)
        {
            // Change the Absolute path to relative path of File Name and add to the List
            imageFileList.Add(new ImageFileInfo { FileName = ResolveUrl(strFileName.Replace(Server.MapPath("/"), "~/")) });
        }
        grdImages.DataSource = imageFileList;
        grdImages.DataBind();
    }
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44