Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows

C#
  protected void Page_Load(object sender, EventArgs e)
    {

        Lstfaculty.Items.Add("Raj");
        Lstfaculty.Items.Add("suresh");
        Lstfaculty.Items.Add("Gopi");
        Lstfaculty.Items.Add("Vignesh");
        Lstfaculty.Items.Add("Ram");

        Lstfacid.Items.Add("1");
        Lstfacid.Items.Add("2");
        Lstfacid.Items.Add("3");
        Lstfacid.Items.Add("4");
        Lstfacid.Items.Add("5");

        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 dropdownfill(DropDownList Dname)
    {
        if (Dname != null)
        {
            
            List<string> strval = new List<string>();
           
            strval.Add("---Select Grading---");
            strval.Add("Excellent");
            strval.Add("Good");
            strval.Add("Fair");
            strval.Add("Poor");
            Dname.DataSource = strval;
            Dname.DataBind();
        }
    }


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


private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("Faculty Name", typeof(string)));
        dt.Columns.Add(new DataColumn("Faculty ID", typeof(string)));
        dt.Columns.Add(new DataColumn("DropDownList1", typeof(string)));
        dt.Columns.Add(new DataColumn("DropDownList2", typeof(string)));
        dt.Columns.Add(new DataColumn("DropDownList3", typeof(string)));
        dt.Columns.Add(new DataColumn("DropDownList4", typeof(string)));
        dt.Columns.Add(new DataColumn("DropDownList5", typeof(String)));


        //Retrieving the faculty name from listbox

        DataTable dt1 = new DataTable();
        Lstfaculty.DataSource = dt1;
        Lstfaculty.DataTextField = "Faculty";
        Lstfaculty.DataValueField = "Faculty";
        Lstfaculty.DataBind();
        int i = 0;
        for (i = 0; i < Lstfaculty.Items.Count; i++)
        {
            dr = dt.NewRow();
            dr["Faculty Name"] = Lstfaculty.Items[i].Text.ToString();
            dt.Rows.Add(dr);
            
        }
        gvfaculty.DataSource = dt;
        gvfaculty.DataBind();

     
        for (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);
        }

        //Retrieving the faculty id from listbox to gridview 

        DataTable dtfacid = new DataTable(); //added
        ListBox2.DataSource = dtfacid;
        ListBox2.DataTextField = "Facid";
        ListBox2.DataValueField = "Facid";
        ListBox2.DataBind();
        int j = 0;
        for (j = 0; j < ListBox2.Items.Count; j++)
        {
            dr = dt.NewRow();
            dr["Faculty ID"] = ListBox2.Items[j].Text.ToString();
            dtfacid.Rows.Add(dr);
        }
      
        Gridviewimage.DataSource = dt;
        Gridviewimage.DataBind(); 

    }


when i execute the above code in gridview the faculty id is not displaying in the gridview.

please help me what is the problem in my code.


Regards,
Narasiman P
Posted
Comments
njammy 14-Jan-14 11:20am    
Please post your markup as well.
First of all I would request you to accept the answer at i want to add listbox and dropdownlist in gridivew, as the answer gave you the idea.

Now, I am looking at this question and trying to find out the problem, if I can. In the mean time, accept that answer. :)

Thanks,
Tadit

1 solution

First, you naming conventions are horrendous. Don't leave dropdowns with default names. Give them meaningful names.

2nd you have this in the page load,

C#
Lstfacid.Items.Add("1");
     Lstfacid.Items.Add("2");
     Lstfacid.Items.Add("3");
     Lstfacid.Items.Add("4");
     Lstfacid.Items.Add("5");


and this in the set data

DataTable dtfacid = new DataTable(); //added
 ListBox2.DataSource = dtfacid;
 ListBox2.DataTextField = "Facid";
 ListBox2.DataValueField = "Facid";
 ListBox2.DataBind();


Your dtfacid DataTable is empty, and that is why you are not seeing anything.
Your data is in Lstfacid. Either bind to that or put your data into the dtfacid object.
 
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