使用Csharp将图像从文件夹显示到gridview [英] Showing images from folder to gridview using Csharp

查看:94
本文介绍了使用Csharp将图像从文件夹显示到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在E文件夹中存储Ravi,Gopi和Sai图像。


在GridView中运行模式中的
显示教师名称,教师ID和教师图像。



我的代码如下

in E folder Ravi,Gopi and Sai images are stored.

in run mode in GridView Faculty Name,Faculty ID and Faculty image are displayed.

My code as follows

 protected void Page_Load(object sender, EventArgs e)
    {
        Lstfaculty.Items.Add("Ravi");
        Lstfaculty.Items.Add("Gopi");
        Lstfacid.Items.Add("1");
        Lstfacid.Items.Add("2");
        Lstfaculty.Visible = false;
        Lstfacid.Visible = false;
        SetInitialRow();
    }


private ArrayList GetData()
    {
        ArrayList arr = new ArrayList();
        arr.Add(new ListItem("Excellent", "1"));
        arr.Add(new ListItem("Good", "2"));
        arr.Add(new ListItem("Fair", "3"));
        arr.Add(new ListItem("Poor", "4"));
        return arr;
    }

    
    private void FillDropdownlist(DropDownList ddl)
    {
        ArrayList arr = GetData();
        foreach (ListItem item in arr)
        {
            ddl.Items.Add(item);
        }
    }

  private void SetInitialRow()
    {
        //Retrieving the faculty name from listbox

        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add("Faculty Name", typeof(string));
        dt.Columns.Add("Faculty ID", typeof(string));
        dt.Columns.Add("Images", typeof(string));
        for (int i = 0; i < Lstfaculty.Items.Count; i++)
        {
            dr = dt.NewRow();
            dr["Faculty Name"] = Lstfaculty.Items[i].Text.ToString();
            dr["Faculty ID"] = Lstfacid.Items[i].Text;
            dt.Rows.InsertAt(dr, i);
        }
        
        DirectoryInfo di = new DirectoryInfo(Server.MapPath("Images"));
        foreach (FileInfo fi in di.GetFiles())
        {
            dr = dt.NewRow();
            string fileName = Path.GetFileName(fi.FullName);
            dr["Faculty Name"] = fileName;
            dr["Images"] = "~/Images/" + fileName;
            dt.Rows.Add(dr);
        }
        gvfaculty.DataSource = dt;
        gvfaculty.DataBind();
         
        for (int i = 0; i < Lstfaculty.Items.Count; i++)
        {
            DropDownList ddl1 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList1");
            DropDownList ddl2 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList2");
            DropDownList ddl3 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList3");
            DropDownList ddl4 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList4");
            DropDownList ddl5 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList5");
            FillDropdownlist(ddl1);
            FillDropdownlist(ddl2);
            FillDropdownlist(ddl3);
            FillDropdownlist(ddl4);
            FillDropdownlist(ddl5);
        }

    }

在Gridview中按如下方式更正输出



教师姓名教师ID图像



Ravi 1 Ravi图片

Gopi 2 Gopi图片





但是当我在Gridview中按如下方式执行上面的代码输出时。



学院名称教师ID图像



Ravi 1 Ravi图片

Gopi 2 Gopi图片



Ravi.jpg~ / Images / Ravi.jpg Ravi image

Gopi.jpg~ / Images / Gopi.jpg Gopi image

Sai.jpg~ / Images / Sai.jpg Sai image


在gridview中
我想只显示Fauclty图像而不是图像的扩展名或路径。



在E文件夹中Ravi,Gopi和Sai图像存储。

在gridview中,Ravi和Gopi Faculty名称在gridview中。



因此在Gridview中我想只显示Ravi和Gopi教师图像,无需在gridview中显示Sai图像。



其中fauclty id在gridview中,教师ID图像仅显示在gridview中。





我的代码中有什么问题。我该怎么办?



问候,

Narasiman P

Correct output as follows in Gridview

Faculty Name Faculty ID Images

Ravi 1 Ravi image
Gopi 2 Gopi Image


But when i execute my above code output as follows in Gridview.

Faculty Name Faculty ID Images

Ravi 1 Ravi image
Gopi 2 Gopi Image

Ravi.jpg ~/Images/Ravi.jpg Ravi image
Gopi.jpg ~/Images/Gopi.jpg Gopi image
Sai.jpg ~/Images/Sai.jpg Sai image

in gridview i want to display only Fauclty images only and not the extension or path of the image.

And in E folder Ravi,Gopi and Sai images are stored.
In the gridview Ravi and Gopi Faculty Name are there in the gridview.

So in the Gridview i want to display only Ravi and Gopi Faculty images and no need to display the Sai image in the gridview.

which fauclty id is there in the gridview that faculty id images only to be displayed in the gridview.


what is the problem in my code.how can i do?

Regards,
Narasiman P

推荐答案

这个 [ ^ ]可能会帮助你。
This[^] might help you out.


像这样修改你的代码..





Modify your code like this..


private void SetInitialRow()
      {
          //Retrieving the faculty name from listbox

          DataTable dt = new DataTable();
          DataRow dr = null;
          dt.Columns.Add("Faculty Name", typeof(string));
          dt.Columns.Add("Faculty ID", typeof(string));
          dt.Columns.Add("Images", typeof(string));

          DirectoryInfo di = new DirectoryInfo(Server.MapPath("Images"));
             var Files = di.GetFiles().Select(k => k.Name).ToList();
          for (int i = 0; i < Lstfaculty.Items.Count; i++)
          {
              dr = dt.NewRow();
              string facultyName = Lstfaculty.Items[i].Text.ToString();
              dr["Faculty Name"] = facultyName;
              string fileName = Files.FirstOrDefault(k => facultyName == k.Substring(0, k.LastIndexOf('.')));
              dr["Faculty ID"] = Lstfacid.Items[i].Text;
              dr["Images"] = "~/Images/" + fileName;
              dt.Rows.Add(dr);
          }
          gvfaculty.DataSource = dt;
          gvfaculty.DataBind();

          for (int i = 0; i < Lstfaculty.Items.Count; i++)
          {
              DropDownList ddl1 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList1");
              DropDownList ddl2 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList2");
              DropDownList ddl3 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList3");
              DropDownList ddl4 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList4");
              DropDownList ddl5 = (DropDownList)gvfaculty.Rows[i].FindControl("DropDownList5");
              FillDropdownlist(ddl1);
              FillDropdownlist(ddl2);
              FillDropdownlist(ddl3);
              FillDropdownlist(ddl4);
              FillDropdownlist(ddl5);
          }

      }


使用预览功能从Web应用程序添加和替换图片 [ ^ ]


这篇关于使用Csharp将图像从文件夹显示到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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