GridView(DataGrid)中使用DropDownList,附代码,测试有效

<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="gridView1_RowDataBound">
            <Columns>
                <asp:BoundField HeaderText="id" DataField="num" DataFormatString="{0}" >
                    <ItemStyle Width="150px" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="drpID">
                    <ItemTemplate>
                        <asp:DropDownList ID="drpNum" runat="server" Width="150px" DataTextField="num" DataValueField="num" DataSource=<%# GetDataSet() %> AutoPostBack="True">
                        </asp:DropDownList>
                    </ItemTemplate>               
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

//****   自已写的DataTable    ************/

    public DataTable GetDataSet()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("num");
        object[] objs = new object[6];
        for (int i = 0; i <= 5; i++)
        {
            objs[i] = new object();
            objs[i] = i.ToString();
            dt.Rows.Add(objs[i]);
        }
        return dt;
    }


    protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList drp = (DropDownList)e.Row.FindControl("drpNum");
            drp.SelectedValue = e.Row.Cells[0].Text;           
        }
    }

DataGrid 是一样的,将其中的e.Row改为e.Item 即可,测试有效。

posted on 2006-07-31 22:23  白亚伟  阅读(849)  评论(3编辑  收藏  举报

导航