• 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 即可,测试有效。

  • 相关阅读:
    由二进制移位想到的
    KDJ指标详解
    PMP考试结束
    转K线理论初级二
    日本地震效应
    Baseline之流水先生的见解
    KDJ判断原则
    转K线理论初级一
    管理学法则
    今天提到KW,特此@Mark一下
  • 原文地址:https://www.cnblogs.com/brawei/p/464450.html
Copyright © 2020-2023  润新知