Gridview与下拉列表编辑无效正在恢复dropdwon列表的第一个值

时间:2018-05-15 12:57:53

标签: asp.net gridview edit dropdown

您好我最终确定使用内部联接表中的下拉列表创建gridview添加,编辑和更新。编辑中的一切工作正在将下拉列表恢复为其第一个值,我希望当用户按下编辑以显示附加值的名称时 提前致谢 那是我的代码:

                    <asp:TemplateField >
                         <ItemStyle Width="143px" />
        <HeaderStyle  Width="143px" />
                         <HeaderTemplate >
         <asp:Label ID="Label4" runat="server" Text="Customer Name"></asp:Label><br /><br /><br />
        <asp:DropDownList ID="fddlCName" Width="145px" DataSourceID="SqlDataSource5"  DataTextField="Customer_Name"
                        DataValueField="C_ID" SelectedValue='<%# Bind("C_ID") %>'
                        Runat="server" AppendDataBoundItems="True"><asp:ListItem Value="-1">---Select Customer---</asp:ListItem></asp:DropDownList>
                         </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblCName" runat="server" Text='<%#Eval("Customer_Name")%>'> </asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="eddlCName" DataSourceID="SqlDataSource5" Width="145px" DataTextField="Customer_Name"
                        DataValueField="C_ID" runat="server" >
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <FooterTemplate>

                        </FooterTemplate>
                    </asp:TemplateField>
                <asp:TemplateField >
                 <ItemStyle Width="140px" />
    <HeaderStyle  Width="140px" />
                 <HeaderTemplate>
     <asp:Label ID="Label5" runat="server" Text="Product"></asp:Label><br /><br /><br />
                 <asp:DropDownList ID="fddlMmodel" DataSourceID="SqlDataSource4" Width="138px"  DataTextField="Machine_Model"
                    DataValueField="Product_ID" SelectedValue='<%# Bind("Product_ID") %>' AppendDataBoundItems="True"
                    Runat="server"><asp:ListItem Value="-1">---Select Product---</asp:ListItem></asp:DropDownList>
                     </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblMmodel" runat="server" Text='<%#Eval("Machine_Model")%>'> </asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="eddlMmodel" DataSourceID="SqlDataSource4"  DataTextField="Machine_Model"
                    DataValueField="Product_ID" Width="138px" Runat="server"></asp:DropDownList>
                    </EditItemTemplate>
                    <FooterTemplate>

                    </FooterTemplate>
                </asp:TemplateField>


    <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:CobornSalesDBConnectionString %>"
 SelectCommand="SELECT distinct Status_ID, Status FROM STATUSES"></asp:SqlDataSource>

         <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:CobornSalesDBConnectionString %>"
 SelectCommand="SELECT distinct Product_ID, Machine_Model FROM PRODUCTS "></asp:SqlDataSource>

我的代码背后为gridview

的内连接
if (!IsPostBack)
        {
            userSales();


        }

    }


    private static DataTable GetData(string query)
    {
        string strConnString = ConfigurationManager.ConnectionStrings["CobornSalesDBConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = query;
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }

    public void userSales()
    {
        string Agent_ID = Session["Agent_ID"].ToString();
        con.Open();
        if (Session["Agent_ID"] != null)
        {
            SqlCommand cmd = new SqlCommand("SELECT SalesActivity.A_ID,SalesActivity.Date, SalesActivity.Quatation_Number,CUSTOMER.Agent_ID, CUSTOMER.Customer_Name, PRODUCTS.Machine_Model, SalesActivity.Quantity, 
                SalesActivity.valueGBR, SalesActivity.valueEUR,  SalesActivity.weightedValue, STATUSES.Status, SalesActivity.estDecisionDate, SalesActivity.PromisedDeliveryDate FROM [SalesActivity] 
                INNER JOIN [CUSTOMER] ON SalesActivity.C_ID = CUSTOMER.C_ID INNER JOIN [PRODUCTS] ON SalesActivity.Product_ID = PRODUCTS.Product_ID INNER JOIN STATUSES ON SalesActivity.Status_ID = STATUSES.Status_ID
                where CUSTOMER.Agent_ID = @Agent_ID", con);
            cmd.Parameters.AddWithValue("@Agent_ID", Agent_ID);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());  // if record not found then returning a blank table structure
                GridView1.DataSource = ds;
                GridView1.DataBind();
                int columncount = GridView1.Rows[0].Cells.Count;
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
                GridView1.Rows[0].Cells[0].Text = "No Records Found";
            }
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex; //this open new index that is edit mode
        userSales();
    }  


    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;   //after cancel button want go to one index back that's y -1
        userSales();
    }
  protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox ftxtvalueGBR = (TextBox)GridView1.HeaderRow.FindControl("ftxtvalueGBR");

        DropDownList fddlStatus = GridView1.HeaderRow.FindControl("fddlStatus") as DropDownList;
        TextBox ftxtweightedValue = (TextBox)GridView1.HeaderRow.FindControl("ftxtweightedValue");


        double weightedValue=0;
        double contractValue = Convert.ToDouble(ftxtvalueGBR.Text);


        if (fddlStatus.SelectedValue == "S00001")
        {
            weightedValue = 0;
        }
        else if (fddlStatus.SelectedValue == "S00002")
        {
            weightedValue = ((contractValue  *  0.25));
        }
        else if (fddlStatus.SelectedValue == "S00003")
        {
            weightedValue = ((contractValue  * 0.50));
        }
        else if (fddlStatus.SelectedValue == "S00004")
        {
            weightedValue = (contractValue);
        }
        else if (fddlStatus.SelectedValue == "S00005")
        {
            weightedValue = 0;
        }
        ftxtweightedValue.Text = weightedValue.ToString();

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string Agent_ID = Session["Agent_ID"].ToString();
        TextBox ftxtDate = (TextBox)GridView1.HeaderRow.FindControl("ftxtDate");
        TextBox ftxtQno = (TextBox)GridView1.HeaderRow.FindControl("ftxtQno");
        DropDownList fddlCName = GridView1.HeaderRow.FindControl("fddlCName") as DropDownList;
        DropDownList fddlMmodel = GridView1.HeaderRow.FindControl("fddlMmodel") as DropDownList;
        TextBox ftxtQuantity = (TextBox)GridView1.HeaderRow.FindControl("ftxtQuantity");
        TextBox ftxtvalueGBR = (TextBox)GridView1.HeaderRow.FindControl("ftxtvalueGBR");
        TextBox ftxtvalueEUR = (TextBox)GridView1.HeaderRow.FindControl("ftxtvalueEUR");
        TextBox ftxtweightedValue = (TextBox)GridView1.HeaderRow.FindControl("ftxtweightedValue");
        DropDownList fddlStatus = GridView1.HeaderRow.FindControl("fddlStatus") as DropDownList;
        TextBox ftxtestDecisionDate = (TextBox)GridView1.HeaderRow.FindControl("ftxtestDecisionDate");
        TextBox ftxtPromisedDeliveryDate = (TextBox)GridView1.HeaderRow.FindControl("ftxtPromisedDeliveryDate");


        con.Open();
        SqlCommand cmd = new SqlCommand("INSERT INTO SalesActivity( Date, Quatation_Number, C_ID, Product_ID, Quantity, valueGBR, valueEUR, weightedValue, Status_ID, estDecisionDate, PromisedDeliveryDate)  VALUES( @Date, @Quatation_Number, @C_ID, @Product_ID, @Quantity, @valueGBR, @valueEUR,  @weightedValue, @Status_ID, @estDecisionDate, @PromisedDeliveryDate) ", con);
        cmd.Parameters.AddWithValue("@Date", DateTime.Parse(ftxtDate.Text));
        cmd.Parameters.AddWithValue("@Quatation_Number", ftxtQno.Text.Trim());
        cmd.Parameters.AddWithValue("@C_ID", fddlCName.SelectedValue);
        cmd.Parameters.AddWithValue("@Product_ID", fddlMmodel.SelectedValue);
        cmd.Parameters.AddWithValue("@Quantity", ftxtQuantity.Text.Trim());
        cmd.Parameters.AddWithValue("@Status_ID", fddlStatus.SelectedValue);
        cmd.Parameters.AddWithValue("@estDecisionDate", DateTime.Parse(ftxtestDecisionDate.Text));
        cmd.Parameters.AddWithValue("@PromisedDeliveryDate", DateTime.Parse(ftxtPromisedDeliveryDate.Text));
        if (string.IsNullOrEmpty(ftxtvalueGBR.Text))
        { cmd.Parameters.Add(new SqlParameter("@valueGBR", DBNull.Value)); }
        else
        { cmd.Parameters.Add(new SqlParameter("@valueGBR", ftxtvalueGBR.Text)); }

        if (string.IsNullOrEmpty(ftxtvalueEUR.Text))
        { cmd.Parameters.Add(new SqlParameter("@valueEUR", DBNull.Value)); }
        else
        { cmd.Parameters.Add(new SqlParameter("@valueEUR", ftxtvalueEUR.Text)); }

        if (string.IsNullOrEmpty(ftxtweightedValue.Text))
        { cmd.Parameters.Add(new SqlParameter("@weightedValue", DBNull.Value)); }
        else
        { cmd.Parameters.Add(new SqlParameter("@weightedValue", ftxtweightedValue.Text)); }


        int result = cmd.ExecuteNonQuery();
        con.Close();

        if (result == 1)
        {
            userSales();
            Response.Write("<script language=javascript>alert('" + ftxtQno.Text + "'+' Sale Details inserted successfully');</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('" + ftxtQno.Text + "'+' Sale Details not inserted');</script>");
        }



    }


    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        string Activity_ID = GridView1.DataKeys[e.RowIndex].Values["A_ID"].ToString();
        TextBox etxtDate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("etxtDate");
        TextBox etxtQno = (TextBox)GridView1.Rows[e.RowIndex].FindControl("etxtQno");
        DropDownList eddlCName = GridView1.Rows[e.RowIndex].FindControl("eddlCName") as DropDownList;
        DropDownList eddlMmodel = GridView1.Rows[e.RowIndex].FindControl("eddlMmodel") as DropDownList;
        TextBox etxtQuantity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("etxtQuantity");
        TextBox etxtvalueGBR = (TextBox)GridView1.Rows[e.RowIndex].FindControl("etxtvalueGBR");
        TextBox etxtvalueEUR = (TextBox)GridView1.Rows[e.RowIndex].FindControl("etxtvalueEUR");
        TextBox etxtweightedValue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("etxtweightedValue");
        DropDownList eddlStatus = GridView1.Rows[e.RowIndex].FindControl("eddlStatus") as DropDownList;
        TextBox etxtestDecisionDate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("etxtestDecisionDate");
        TextBox etxtPromisedDeliveryDate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("etxtPromisedDeliveryDate");


        con.Open();
        SqlCommand cmd = new SqlCommand("update SalesActivity set Date=@Date,Quatation_Number=@Quatation_Number, C_ID = @C_ID, Product_ID = @Product_ID,Quantity = @Quantity, valueGBR = @valueGBR, valueEUR = @valueEUR, weightedValue = @weightedValue, Status_ID = @Status_ID,estDecisionDate = @estDecisionDate, PromisedDeliveryDate = @PromisedDeliveryDate  where A_ID=@A_ID ", con);
        cmd.Parameters.AddWithValue("@A_ID", Activity_ID);
        cmd.Parameters.AddWithValue("@Date", DateTime.Parse(etxtDate.Text));
        cmd.Parameters.AddWithValue("@Quatation_Number", etxtQno.Text.Trim());
        cmd.Parameters.AddWithValue("@C_ID", eddlCName.SelectedValue);
        cmd.Parameters.AddWithValue("@Product_ID", eddlMmodel.SelectedValue);
        cmd.Parameters.AddWithValue("@Status_ID", eddlStatus.SelectedValue);
        cmd.Parameters.AddWithValue("@estDecisionDate", DateTime.Parse(etxtestDecisionDate.Text));
        cmd.Parameters.AddWithValue("@PromisedDeliveryDate", DateTime.Parse(etxtPromisedDeliveryDate.Text));

        if (string.IsNullOrEmpty(etxtvalueGBR.Text))
        { cmd.Parameters.Add(new SqlParameter("@valueGBR", DBNull.Value)); }
        else
        { cmd.Parameters.Add(new SqlParameter("@valueGBR", etxtvalueGBR.Text)); }

        if (string.IsNullOrEmpty(etxtQuantity.Text))
        { cmd.Parameters.Add(new SqlParameter("@Quantity", DBNull.Value)); }
        else
        { cmd.Parameters.Add(new SqlParameter("@Quantity", etxtQuantity.Text)); }

        if (string.IsNullOrEmpty(etxtvalueEUR.Text))
        { cmd.Parameters.Add(new SqlParameter("@valueEUR", DBNull.Value)); }
        else
        { cmd.Parameters.Add(new SqlParameter("@valueEUR", etxtvalueEUR.Text)); }

        if (string.IsNullOrEmpty(etxtweightedValue.Text))
        { cmd.Parameters.Add(new SqlParameter("@weightedValue", DBNull.Value)); }
        else
        { cmd.Parameters.Add(new SqlParameter("@weightedValue", etxtweightedValue.Text)); }



        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("<script language=javascript>alert('" + etxtQno.Text + "'+' Details Updated successfully!');</script>");
        GridView1.EditIndex = -1;
        userSales();
    }

    protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;

        userSales();
    }

    }
  }

0 个答案:

没有答案