我试过,但我得到输出添加列表框在Gridview中的Ddropdownlist [英] I Tried But I Am Get The Output Adding Listbox An Ddropdownlist In Gridview

查看:54
本文介绍了我试过,但我得到输出添加列表框在Gridview中的Ddropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下



My code as follows

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

    }





当我在gridview中执行上述代码时,教师ID未显示在gridview中。



请帮我解决我的代码中的问题。





问候,

Narasiman P



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

推荐答案

首先,你的命名约定是可怕的。不要将下拉列表保留为默认名称。给他们有意义的名字。



第二,你在页面加载中有这个,



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,

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





您的dtfacid DataTable为空,这就是您没有看到任何内容的原因。
您的数据在Lstfacid中。绑定到该数据或将数据放入dtfacid对象。



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.


这篇关于我试过,但我得到输出添加列表框在Gridview中的Ddropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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