Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何从数据库绑定gridview中的下拉列表,并对其进行编辑和更新_Asp.net_Sql_Gridview - Fatal编程技术网

Asp.net 如何从数据库绑定gridview中的下拉列表,并对其进行编辑和更新

Asp.net 如何从数据库绑定gridview中的下拉列表,并对其进行编辑和更新,asp.net,sql,gridview,Asp.net,Sql,Gridview,我想将gridview中的下拉列表与来自数据库的datasiursce绑定,如果需要,我还想对其进行编辑和更新 数据源包含我要获取的数据,它不绑定 if (gvItem.Rows.Count > 0) { for (int i = 0; i < gvItem.Rows.Count; i++) { DropDownList sup =

我想将gridview中的下拉列表与来自数据库的datasiursce绑定,如果需要,我还想对其进行编辑和更新

数据源包含我要获取的数据,它不绑定

            if (gvItem.Rows.Count > 0)
            {
                for (int i = 0; i < gvItem.Rows.Count; i++)
                {

                    DropDownList sup = (DropDownList)gvItem.Rows[i].FindControl("ddlsup");

                    baplan.Condition = "sup";
                    DataSet dss = new DataSet();
                    dss = baplan.getpro();

                    sup.DataSource = dss;
                    sup.DataBind();
                    sup.DataTextField = "Name";
                    sup.DataValueField = "Id";
                }
            }
if(gvItem.Rows.Count>0)
{
对于(int i=0;i
if(gvItem.Rows.Count>0)
{
对于(int i=0;i
如果您仍在寻找答案,以下是一个详细示例:

输出

ASPX 这里的“BindData”是什么?
if (gvItem.Rows.Count > 0)
    {
        for (int i = 0; i < gvItem.Rows.Count; i++)
        {

            DropDownList sup = (DropDownList)gvItem.Rows[i].FindControl("ddlsup");

            baplan.Condition = "sup";
            DataSet dss = new DataSet();
            dss = baplan.getpro();

            sup.DataSource = dss;

            sup.DataTextField = "Name";
            sup.DataValueField = "Id";
            sup.DataBind();


            }
        }
    <asp:GridView runat="server" ID="gridView" AutoGenerateColumns="False" 
        onrowdatabound="gridView_RowDataBound" 
        onrowcancelingedit="gridView_RowCancelingEdit" 
        onrowediting="gridView_RowEditing" onrowupdating="gridView_RowUpdating">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField HeaderText="First Name" DataField="FirstName" />
            <asp:BoundField HeaderText="Last Name" DataField="LastName" />
            <asp:TemplateField HeaderText="Country">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Country.Name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList runat="server" ID="ddlCountries" DataTextField="Name" DataValueField="ID">
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.BindGrid();
        }
    }
    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        var t = e.Row.RowType;

        if (t == DataControlRowType.DataRow)
        {
            if (this.gridView.EditIndex >= 0 && e.Row.RowIndex == this.gridView.EditIndex)
            {
                var d = e.Row.FindControl("ddlCountries") as DropDownList;
                var em = e.Row.DataItem as Employee;

                d.DataSource = this.GetCountries();
                d.DataBind();

                d.SelectedValue = em.Country.ID.ToString();
            }
        }
    }
    protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.gridView.EditIndex = e.NewEditIndex;
        this.BindGrid();
    }

    protected void gridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.gridView.EditIndex = -1;
        this.BindGrid();
    }

    protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var row = this.gridView.Rows[e.RowIndex];
        var c = (DropDownList)row.FindControl("ddlCountries");
        var newValue = c.SelectedValue;

        // save the changes

        this.gridView.EditIndex = -1;
        this.BindGrid();
    }
 protected void grdAt_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
          

            DropDownList ddl_GrdDtype = (DropDownList)e.Row.FindControl("ddlGrdDtype");
            Label lbl_grdStatus = (Label)e.Row.FindControl("lblgrdStatus");


            DataTable dt = attBL.DutyTypeMasterDDLBindForAttendance_L();
            if (dt.Rows.Count > 0)
            {
                ddl_GrdDtype.DataSource = dt;
                ddl_GrdDtype.DataValueField = "DutyCode";
                ddl_GrdDtype.DataTextField = "DutyTypeNick";
                ddl_GrdDtype.DataBind();
                ddl_GrdDtype.Items.Insert(0, "Select");
                
            }
            ddl_GrdDtype.SelectedItem.Text = lbl_grdStatus.Text;
        }

    }