如何获取gridView 中的dorpDownList 名称(ID)??
dropDownList 是gridView中的一模版列!!
我想在gridview赋完datasource后,
能让dropdownlist 实现
    dropdownlist.DataSource=数据源
    dropdownlist.DataValueField=数据源
    dropdownlist.DataTextField=数据源
高手指点!!!

解决方案 »

  1.   


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            dorpDownList  名称= (dorpDownList)GridView1.FindControl("名称") ;
        }
      

  2.   


    foreach(Control c in GridView1.Rows[rowindex].Cells[colindex].Controls)
    {
        if (c is DropDownList)
        {
            DropDownList ddl = c as DropDownList;
            //ddl.ID
        }
    }
      

  3.   


    DropDownList dr= (DropDownList )GridView1.Rows[i].FindControl("DropDownList的名字");
      

  4.   

            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowIndex >= 0)
                {
                    //填充編號
                    Label lblNo = (Label)e.Row.FindControl("lblNo");
                    if (i < 10)
                    {
                        lblNo.Text = "0" + i.ToString() + ".";
                    }
                    else
                    {
                        lblNo.Text = i.ToString() + ".";
                    }
                    i++;                if (e.Row.RowIndex >= 1)
                    {
                        e.Row.Cells[2].Text = GridView1.Rows[e.Row.RowIndex - 1].Cells[2].Text;                }                Label Label1 = (Label)e.Row.FindControl("Label1"); //繳款項目不能為空.如果為空則是 新增的.顯示 新增控件
                    if (String.IsNullOrEmpty(Label1.Text.Trim()))
                    {
    //這裏是對樓主問題的答復。。
                        DropDownList ddl = (DropDownList)e.Row.FindControl("ddlProject"); //交款項目顯示
                        DataSet ds = getddlProject();
                        ddl.DataSource = ds;
                        ddl.DataTextField = "codename";
                        ddl.DataValueField = "codeno";
                        ddl.DataBind();                    ddl.Visible = true;
                        Label1.Visible = false;                    TextBox txtCount = (TextBox)e.Row.FindControl("txtCount"); //應繳費用顯示
                        txtCount.Visible = true;
                        Label Label2 = (Label)e.Row.FindControl("Label2");
                        Label2.Visible = false;                    Button btn_Save = (Button)e.Row.FindControl("btn_Save"); //顯示保存按鈕
                        btn_Save.Visible = true;                    Label lblExplain = (Label)e.Row.FindControl("lblExplain"); //說明
                        lblExplain.Visible = false;
                        TextBox txtExplain = (TextBox)e.Row.FindControl("txtExplain");
                        txtExplain.Visible = true; 
                    }            }
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onmouseover"] = "lineHighLight(this)";
                    if ((e.Row.RowIndex % 2) == 1)
                    {
                        e.Row.Attributes["onmouseout"] = "lineHighLightBack1(this)";
                    }
                    else
                    {
                        e.Row.Attributes["onmouseout"] = "lineHighLightBack(this)";
                    }
                }
            }
      

  5.   

    C#代码:
    protected void Page_Load(object sender, EventArgs e)
        {
            //DateTime time_now = TimeNow.AddDays(-TimeNow.Day); 
            //Response.Write(time_now.ToString());        GridView1.DataSource = getDataTable();
            GridView1.DataBind();
        }    public DataTable getDataTable()
        {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("ID", typeof(int));
            dt.Columns.Add(dc);
            DataColumn dc2 = new DataColumn("Name", typeof(string));
            dt.Columns.Add(dc2);        DataRow dr = dt.NewRow();
            dr["ID"] = 1;
            dr["Name"] = "aaaaa";
            dt.Rows.Add(dr);        DataRow dr2 = dt.NewRow();
            dr2["ID"] = 2;
            dr2["Name"] = "bbb";
            dt.Rows.Add(dr2);        return dt;
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //假设你想得到不同的ddl里的值,这个就需要你在得到数据的方法里传入参数。
                DropDownList ddl = ((DropDownList)e.Row.Cells[2].FindControl("DropDownList1"));
                ddl.DataSource = getDataTable();
                ddl.DataTextField = "Name";
                ddl.DataValueField = "ID";
                ddl.DataBind();
            }
        }
    页面文件内容:
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" />
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:TemplateField HeaderText="操作">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>