Gridview排序导致RowDataBound无法正常运行

时间:2013-08-23 17:53:49

标签: asp.net aspxgridview

我有一个gridview可以正常使用我的每个行命令,但是,一旦它被排序,它会导致rowdatabound事件使用不正确的值,而不是排序后的行中的实际值。任何帮助将不胜感激!

这是我背后的代码。

 //Verify and Update Record
protected void UnverifiedSalesGV_RowCommand(object sender, GridViewCommandEventArgs e)
{
    buttonCommand = e.CommandName;
    MessageLBL.Text = "";

    if (e.CommandName == "VerifyRecord")
    {

        //Get record ID
        string salesID = UnverifiedSalesGV.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();

        //Get productID 
        SalesData getSalesRecord = new SalesData();
        getSalesRecord.GetRowValuesSalesID(salesID);

        string productID = getSalesRecord.productID;


        if (productID != "38")
        {
            verifyRenewal = false;
            try
            {
                UpdateSalesRecordSDS.UpdateCommand = "UPDATE Sales SET Verified = @Verified WHERE ID = @ID";
                UpdateSalesRecordSDS.UpdateParameters["ID"].DefaultValue = UnverifiedSalesGV.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
                UpdateSalesRecordSDS.UpdateParameters["Verified"].DefaultValue = true.ToString();
                UpdateSalesRecordSDS.Update();

                UnverifiedSalesGV.DataBind();
                VerifiedSalesGV.DataBind();
            }
            catch (Exception ex)
            {
                MessageLBL.ForeColor = Color.Red;
                MessageLBL.Text = "Could not verify sale. Message: " + ex.Message;
            }
        }
        else
        {
            //Get row index.
            UnverifiedSalesGV.SetEditRow(Convert.ToInt32(e.CommandArgument));
            verifyRenewal = true;
        }
    }
    if (e.CommandName == "UpdateProduct")
    {

        DropDownList productValue = (DropDownList)UnverifiedSalesGV.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("RenewalProductDDL");
        if (productValue.SelectedIndex != 0)
        {
            try
            {
                UpdateSalesRecordSDS.UpdateCommand = "UPDATE Sales SET ProductID = @ProductID, Verified = @Verified WHERE ID = @ID";
                UpdateSalesRecordSDS.UpdateParameters["ID"].DefaultValue = UnverifiedSalesGV.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
                UpdateSalesRecordSDS.UpdateParameters["ProductID"].DefaultValue = productValue.SelectedValue;
                UpdateSalesRecordSDS.UpdateParameters["Verified"].DefaultValue = true.ToString();
                UpdateSalesRecordSDS.Update();

                UnverifiedSalesGV.DataBind();
                UnverifiedSalesGV.EditIndex = -1;
                VerifiedSalesGV.DataBind();
            }
            catch (Exception ex)
            {
                MessageLBL.ForeColor = Color.Red;
                MessageLBL.Text = "Could not verify sale. Message: " + ex.Message;
            }
        }
        else
        {
            MessageLBL.ForeColor = Color.Red;
            MessageLBL.Text = "Please select renewal type.";
        }

    }
    if (e.CommandName == "UpdateRecord")
    {
        //Get date and user info
        DateTime getDate = System.DateTime.Now;
        MembershipUser user = Membership.GetUser();
        string activeuser = user.UserName;

        try
        {
            //Get dropdown and textbox values
            string commisionMonth;
            string grossSalesAmount;
            string netSalesAmount;
            string notes;

            TextBox grossSalesValue = (TextBox)UnverifiedSalesGV.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("GrossSalesTXT");
            TextBox netSalesValue = (TextBox)UnverifiedSalesGV.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("NetSalesTXT");
            TextBox notesValue = (TextBox)UnverifiedSalesGV.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("NotesTXT");
            DropDownList commissionMonthValue = (DropDownList)UnverifiedSalesGV.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("CommissionMonthDDL");
            grossSalesAmount = grossSalesValue.Text;
            netSalesAmount = netSalesValue.Text;
            commisionMonth = commissionMonthValue.SelectedValue;
            notes = notesValue.Text;

            UnverifiedSalesSDS.UpdateCommand = "UPDATE [Sales] SET [GrossSalesAmount] = @GrossSalesAmount, [NetSalesAmount] = @NetSalesAmount, [Notes] = @Notes, [CommissionMonth] = @CommissionMonth, [DateLastModified] = @DateLastModified, [UserLastModified] = @UserLastModified WHERE [ID] = @ID";
            UnverifiedSalesSDS.UpdateParameters["GrossSalesAmount"].DefaultValue = grossSalesAmount;
            UnverifiedSalesSDS.UpdateParameters["NetSalesAmount"].DefaultValue = netSalesAmount;
            UnverifiedSalesSDS.UpdateParameters["CommissionMonth"].DefaultValue = commisionMonth;
            UnverifiedSalesSDS.UpdateParameters["Notes"].DefaultValue = notes;
            UnverifiedSalesSDS.UpdateParameters["DateLastModified"].DefaultValue = getDate.ToString();
            UnverifiedSalesSDS.UpdateParameters["UserLastModified"].DefaultValue = activeuser;
            UnverifiedSalesSDS.UpdateParameters["ID"].DefaultValue = UnverifiedSalesGV.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();


            UnverifiedSalesSDS.Update();
            UnverifiedSalesGV.DataBind();
            UnverifiedSalesGV.EditIndex = -1;


        }
        catch (Exception ex)
        {
            MessageLBL.ForeColor = Color.Red;
            MessageLBL.Text = "Could not update record. Message: " + ex.Message;
        }
    }

}

//Get product
protected void UnverifiedSalesGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowState == DataControlRowState.Edit)
    {

        if (buttonCommand == "VerifyRecord")
        {
            //Get record ID
            string salesID = UnverifiedSalesGV.DataKeys[Convert.ToInt32(e.Row.DataItemIndex)].Value.ToString();

            //Get productID 
            SalesData getSalesRecord = new SalesData();
            getSalesRecord.GetRowValuesSalesID(salesID);

            string productID = getSalesRecord.productID;

            if (productID == "38")
            {
                //Items to Hide/Display
                Button UpdateProductBTN = (Button)e.Row.FindControl("UpdateProductBTN");
                Button UpdateBTN = (Button)e.Row.FindControl("UpdateBTN");
                Label productLBL = (Label)e.Row.FindControl("CurrentProductLBL");
                DropDownList productDDL = (DropDownList)e.Row.FindControl("RenewalProductDDL");
                Label grossSalesLBL = (Label)e.Row.FindControl("GrossSalesLBL");
                TextBox grossSalesTXT = (TextBox)e.Row.FindControl("GrossSalesTXT");
                Label netSalesLBL = (Label)e.Row.FindControl("NetSalesLBL");
                TextBox netSalesTXT = (TextBox)e.Row.FindControl("NetSalesTXT");
                Label commissionMonthLBL = (Label)e.Row.FindControl("SalesCommissionLBL");
                DropDownList commissionMonthDDL = (DropDownList)e.Row.FindControl("CommissionMonthDDL");
                TextBox notesTXT = (TextBox)e.Row.FindControl("NotesTXT");


                UpdateProductBTN.Visible = true;
                UpdateBTN.Visible = false;
                productLBL.Visible = false;
                productDDL.Visible = true;
                grossSalesLBL.Visible = true;
                grossSalesTXT.Visible = false;
                netSalesLBL.Visible = true;
                netSalesTXT.Visible = false;
                commissionMonthLBL.Visible = true;
                commissionMonthDDL.Visible = false;
                notesTXT.Visible = false;
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我终于能够通过下面的硬编码来解决我的问题了:

 //Hide and show fields
protected void UnverifiedSalesGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowIndex == rowIndex)
    {
        string state = e.Row.RowState.ToString();
        if (state == "Alternate, Edit" || state == "Edit")
        {

            //Get record ID
            string salesID = UnverifiedSalesGV.DataKeys[Convert.ToInt32(e.Row.DataItemIndex)].Value.ToString();

            //Get productID 
            SalesData getSalesRecord = new SalesData();
            getSalesRecord.GetRowValuesSalesID(salesID);

            string productID = getSalesRecord.productID;

            if (productID == "38")
            {
                //Items to Hide/Display
                Button UpdateProductBTN = (Button)e.Row.FindControl("UpdateProductBTN");
                Button UpdateBTN = (Button)e.Row.FindControl("UpdateBTN");
                Label productLBL = (Label)e.Row.FindControl("CurrentProductLBL");
                Label accountManagerLBL = (Label)e.Row.FindControl("AccountManagerLBL");
                DropDownList productDDL = (DropDownList)e.Row.FindControl("RenewalProductDDL");
                DropDownList accountManagerDDL = (DropDownList)e.Row.FindControl("AccountManagerDDL");
                Label grossSalesLBL = (Label)e.Row.FindControl("GrossSalesLBL");
                TextBox grossSalesTXT = (TextBox)e.Row.FindControl("GrossSalesTXT");
                Label netSalesLBL = (Label)e.Row.FindControl("NetSalesLBL");
                TextBox netSalesTXT = (TextBox)e.Row.FindControl("NetSalesTXT");
                Label commissionMonthLBL = (Label)e.Row.FindControl("SalesCommissionLBL");
                DropDownList commissionMonthDDL = (DropDownList)e.Row.FindControl("CommissionMonthDDL");
                TextBox notesTXT = (TextBox)e.Row.FindControl("NotesTXT");
                Label notesLBL = (Label)e.Row.FindControl("NotesLBL");


                UpdateProductBTN.Visible = true;
                UpdateBTN.Visible = false;
                productLBL.Visible = false;
                productDDL.Visible = true;
                accountManagerLBL.Visible = true;
                accountManagerDDL.Visible = false;
                grossSalesLBL.Visible = true;
                grossSalesTXT.Visible = false;
                netSalesLBL.Visible = true;
                netSalesTXT.Visible = false;
                commissionMonthLBL.Visible = true;
                commissionMonthDDL.Visible = false;
                notesTXT.Visible = false;
                notesLBL.Visible = true;
            }
        }
    }
}