1
Reply

display image from folder to gridview using CSharp

narasiman rao

narasiman rao

Jan 18 2014 11:59 AM
945
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);
}
}
when i run 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

Answers (1)
Next Recommended Forum