Click here to Skip to main content
16,018,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
 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);
        }

    }
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
Posted
Updated 18-Jan-14 8:11am
v2

Modify your code like this..


C#
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);
          }

      }
 
Share this answer
 
This[^] might help you out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900