Gridview_RowEditing空值

时间:2011-05-09 22:34:02

标签: vb.net gridview

我有一个gridview控件,我手动绑定数据。当我编辑一行并更新它时,发送的文本框的值始终是旧值。我已经找到了一些线程但是没有运气提取新值。

    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
        </Columns>
    </asp:GridView>



  Private Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim gv As GridView = sender
        For i As Integer = 0 To gv.Columns.Count Step 1
            Dim cell As DataControlFieldCell = gv.Rows(e.RowIndex).Cells(i)
            gv.Columns(0).ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, True)
        Next
        For Each s As DictionaryEntry In e.NewValues
            Debug.Print(s.Key & " | " & s.Value)
        Next

        ds.Tables("testTable").Rows(e.RowIndex).BeginEdit()
       [ ... ]
        ds.Tables("testTable").Rows(e.RowIndex).EndEdit()

        GridView1.EditIndex = -1

        BindData()
    End Sub

我还想指出ExtractValuesFromCell代码只是我最近获取新数据的尝试。在此之前我正在使用类似的东西

Dim tb as TextBox = sender.Rows(e.RowIndex).Cells(1).Controls(0)
Label1.Text = tb.Text

此处还有数据如何开始

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (ds.Tables.Count = 0) Then
        ds.Tables.Add("testTable")
        ds.Tables("testTable").Columns.Add("Driver Name")
        ds.Tables("testTable").Columns.Add("Total")
        ds.Tables("testTable").Columns.Add("# Of Calls")
        ds.Tables("testTable").Columns("Total").ReadOnly = True
        ds.Tables("testTable").Columns("# Of Calls").ReadOnly = True

        Dim newRow As DataRow = ds.Tables("testTable").NewRow()
        newRow("Driver Name") = ""
        newRow("Total") = ""
        newRow("# Of Calls") = ""
        ds.Tables("testTable").Rows.Add(newRow)
    End If
    BindData()
End Sub

2 个答案:

答案 0 :(得分:3)

只有在Not Page.IsPostback时才应对DataBind进行DataBind,否则会覆盖新值。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx

答案 1 :(得分:0)

   public static void filedownload(string Path)
    {
        string str = "";
        FileInfo file = new FileInfo(Path);

        // Checking if file exists
        if (file.Exists)
        {
            // Clear the content of the response
            HttpContext.Current.Response.ClearContent();

            // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

            // Add the file size into the response header
            HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());

            // Set the ContentType
            HttpContext.Current.Response.ContentType = CommonStrings.returnextension(file.Extension.ToLower());

            // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
            HttpContext.Current.Response.WriteFile(file.FullName);

            // End the response
            HttpContext.Current.Response.End();
        }
        else
        {
            // 9-vgrfu8i "File Not Found!";
        }
        // Response.Redirect(e.CommandArgument.ToString());
    }



    public static string GetBarCodeid(ListBox lbListBox)
    {
        string strCode = "";
        if (lbListBox.Items.Count > 0)
        {
            for (int i = 0; i < lbListBox.Items.Count; i++)
            {

                if (lbListBox.Items[i].Selected == true)
                {
                    if (strCode == "")
                    {
                        strCode = lbListBox.Items[i].Value.ToString();
                    }
                    else
                    {
                        strCode += "," + lbListBox.Items[i].Value.ToString();
                    }

                }
            }
        }
        return strCode;

    }

    public static string GetBarCodeName(ListBox lbListBox)
    {
        string strBarCodeName = "";
        if (lbListBox.Items.Count > 0)
        {
            for (int i = 0; i < lbListBox.Items.Count; i++)
            {

                if (lbListBox.Items[i].Selected == true)
                {
                    if (strBarCodeName == "")
                    {
                        strBarCodeName = lbListBox.Items[i].Text.ToString();
                    }
                    else
                    {
                        strBarCodeName += "," + lbListBox.Items[i].Text.ToString();
                    }

                }
            }
        }
        return strBarCodeName;

    }
    public static double GetBarCodeCount(ListBox lbListBox, double Count)
    {
        double intBarCodeCount = 0;
        if (lbListBox.Items.Count > 0)
        {
            for (int i = 0; i < lbListBox.Items.Count; i++)
            {

                if (lbListBox.Items[i].Selected == true)
                {
                    if (intBarCodeCount == 0)
                    {
                        intBarCodeCount = 1;
                    }
                    else
                    {
                        intBarCodeCount++;
                    }

                }
            }
        }
        return intBarCodeCount;

    }

    public static void SelectListBox(ListBox lbListBox, string strBarCodeId)
    {
        ExecuteProcedures ex = new ExecuteProcedures(1, MasterCommonStrings.ConnectionString);
        ex.Parameters.Add("@vcrBarCodeid", SqlDbType.VarChar, 500, strBarCodeId);
        DataTable dt = (DataTable)ex.LoadTableWithProcedure("Proc_Erp_Trn_Get_BarCode_Bata");
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < lbListBox.Items.Count; j++)
                {
                    if (dt.Rows[i]["data"].ToString() == lbListBox.Items[j].Value.ToString())
                    {
                        lbListBox.Items[j].Selected = true;
                        break;
                    }
                }
            }
        }

    }
    public static string GetgridBarCode(DataTable dt, string strColumnName)
    {
        string strBarCodeCount = "";

        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (Convert.ToInt32(dt.Rows[i]["intStatus"]) != 2)
                {
                    if (dt.Rows[i][strColumnName].ToString() != "")
                        if (strBarCodeCount == "")
                        {
                            strBarCodeCount = dt.Rows[i][strColumnName].ToString();
                        }
                        else
                        {
                            strBarCodeCount += dt.Rows[i][strColumnName].ToString();
                        }
                }
            }
        }
        return strBarCodeCount;
    }


    public static DataTable CheckBarCodeExistsOrNot(ListBox lbListbox, string strColumeName, string strReplacegridBarCodeid, DataView dvDescription)
    {
        DataTable dtDescription = new DataTable();
        dtDescription = (DataTable)dvDescription.Table;
        string strgridBarCode = CommonFunctions.GetgridBarCode(dtDescription, strColumeName);
        string strSelectBarCode = CommonFunctions.GetBarCodeid(lbListbox);
        ExecuteProcedures ex = new ExecuteProcedures(3, MasterCommonStrings.ConnectionString);
        ex.Parameters.Add("@vcrgridBarcodeid", SqlDbType.VarChar, 500, strgridBarCode);
        ex.Parameters.Add("@vcrSelectBarcodeid", SqlDbType.VarChar, 500, strSelectBarCode);
        ex.Parameters.Add("@vcrReplaceBarCodeid", SqlDbType.VarChar, 500, strReplacegridBarCodeid);
        DataTable dt = (DataTable)ex.LoadTableWithProcedure("Proc_Erp_Trn_Check_BarCode_Exists_Or_Not");
        return dt;



    }

    public static int BarcodeUpdateIntoBarCodeDrec(string strBarCode_Description_Drec_id, SqlTransaction sqlTran, SqlConnection Con)
    {
        ExecuteProcedures ex = new ExecuteProcedures(1, MasterCommonStrings.ConnectionString);
        ex.Parameters.Add("@intBarCode_Description_Drec_id", SqlDbType.VarChar, 8000, strBarCode_Description_Drec_id);
        int i = Convert.ToInt32(ex.InvokeProcedure("Proc_Erp_Trn_Update_erp_mst_BarCode_Drec", sqlTran, ValueDataType.Number, Con));
        if (i != 0)
        {
            return i;

        }
        else
        {
            return 0;

        }


    }

    public static string AddBarcodeToListBox(TextBox txtScanBarcode, ListBox lbScanBarCode, ListBox lbSystemBarCode)
    {
        string barcodePresent = "";
        if (txtScanBarcode.Text != "")
        {
            foreach (ListItem li in lbSystemBarCode.Items)
            {
                if (li.Text != txtScanBarcode.Text)
                {
                    barcodePresent = "Not In Our System";
                }
                else
                {
                    if (li.Selected == true)
                    {
                        barcodePresent = "Barcode Already Scan";
                        txtScanBarcode.Text = "";
                        txtScanBarcode.Focus();
                        break;
                    }
                    else
                    {
                        lbScanBarCode.Items.Add(txtScanBarcode.Text);
                        li.Selected = true;
                        txtScanBarcode.Text = "";
                        txtScanBarcode.Focus();
                        barcodePresent = "Barcode Scan Successfully";
                        break;
                    }

                }
            }
            if (barcodePresent == "Not In Our System")
            {
                txtScanBarcode.Text = "";
                txtScanBarcode.Focus();
            }
        }
        else
        {
            barcodePresent = "Please Scan Barcode";
        }
        return barcodePresent;

    }

    public static void RemoveBarCodeFromListBox(ListBox lbScanBarCode, ListBox lbSystemBarCode)
    {
        if (lbScanBarCode.Items.Count > 0)
        {
            for (int i = 0; i < lbScanBarCode.Items.Count; i++)
            {
                if (lbScanBarCode.Items[i].Selected)
                {
                    foreach (ListItem li1 in lbSystemBarCode.Items)
                    {
                        if (lbScanBarCode.Items[i].Text == li1.Text)
                        {
                            li1.Selected = false;
                            break;
                        }
                    }
                    lbScanBarCode.Items.Remove(lbScanBarCode.Items[i]);
                }
            }
        }
    }

    public static void AddBarcodeToGridListBox(ListBox lbBarCode, ListBox lbGridScanBarCodeDisplay)
    {
        if (lbBarCode.Items.Count > 0)
        {
            for (int i = 0; i < lbBarCode.Items.Count; i++)
            {
                if (lbBarCode.Items[i].Selected == true)
                {
                    lbGridScanBarCodeDisplay.Items.Add(lbBarCode.Items[i].Text);
                }
            }
        }
    }


     public static void Alert(string strMessage, System.Web.UI.Page PAGE)
    {
        HttpContext Current = HttpContext.Current;
        string strScript = "<script type=text/javascript>alert('" + strMessage + "')</script>";
        if (!PAGE.IsStartupScriptRegistered("Alert"))
        {
            PAGE.RegisterStartupScript("Alert", strScript);
        }
    }
    public static int DeletedTotalDrec(DataView dv, string strTableName, string strColumnName, string Value, SqlTransaction SqlTra, SqlConnection Con)
    {
        DataTable dtDrec = new DataTable();
        dtDrec = (DataTable)dv.Table;
        int i = 0;
        if (dtDrec.Rows.Count > 0)
        {
            ExecuteProcedures one = new ExecuteProcedures(3, MasterCommonStrings.ConnectionString);
            one.Parameters.Add("@TableName", SqlDbType.VarChar, 8000, strTableName);
            one.Parameters.Add("@ColumnName", SqlDbType.VarChar, 8000, strColumnName);
            one.Parameters.Add("@ColumnValue", SqlDbType.VarChar, 8000, Value);
            int b = Convert.ToInt32(one.InvokeProcedure("Proc_Erp_Trn_Deleted_Transction_Total_Drec", SqlTra, ValueDataType.Number, Con));


            for (int j = 0; j < dtDrec.Rows.Count; j++)
            {

                ExecuteProcedures EX = new ExecuteProcedures(19, MasterCommonStrings.ConnectionString);
                EX.Parameters.Add("@TableName", SqlDbType.VarChar, 8000, strTableName);
                EX.Parameters.Add("@FirstColumnName", SqlDbType.VarChar, 8000, dtDrec.Columns[0].ColumnName.ToString());
                EX.Parameters.Add("@SecondColumnName", SqlDbType.VarChar, 8000, dtDrec.Columns[1].ColumnName.ToString());
                EX.Parameters.Add("@ThiredColumnName", SqlDbType.VarChar, 8000, dtDrec.Columns[2].ColumnName.ToString());
                EX.Parameters.Add("@FourColumnName", SqlDbType.VarChar, 8000, dtDrec.Columns[3].ColumnName.ToString());
                EX.Parameters.Add("@FiveColumnName", SqlDbType.VarChar, 8000, dtDrec.Columns[4].ColumnName.ToString());
                EX.Parameters.Add("@SixtColumnName", SqlDbType.VarChar, 8000, dtDrec.Columns[5].ColumnName.ToString());
                EX.Parameters.Add("@SevenColumnName", SqlDbType.VarChar, 8000, dtDrec.Columns[6].ColumnName.ToString());
                EX.Parameters.Add("@EightColumnName ", SqlDbType.VarChar, 8000, dtDrec.Columns[7].ColumnName.ToString());
                EX.Parameters.Add("@NineColumnName", SqlDbType.VarChar, 8000, strColumnName);
                EX.Parameters.Add("@FirstColumnNameValue ", SqlDbType.VarChar, 8000, dtDrec.Rows[j][0].ToString());
                EX.Parameters.Add("@SecondColumnNameValue", SqlDbType.VarChar, 8000, dtDrec.Rows[j][1].ToString());
                EX.Parameters.Add("@ThiredColumnNameValue", SqlDbType.VarChar, 8000, dtDrec.Rows[j][2].ToString());
                EX.Parameters.Add("@FourColumnNameValue ", SqlDbType.VarChar, 8000, dtDrec.Rows[j][3].ToString());
                EX.Parameters.Add("@FiveColumnNameValue", SqlDbType.VarChar, 8000, dtDrec.Rows[j][4].ToString());
                EX.Parameters.Add("@SixtColumnNameValue", SqlDbType.VarChar, 8000, dtDrec.Rows[j][5].ToString());
                EX.Parameters.Add("@SevenColumnNameValue", SqlDbType.VarChar, 8000, dtDrec.Rows[j][6].ToString());
                EX.Parameters.Add("@EightColumnNameValue", SqlDbType.VarChar, 8000, dtDrec.Rows[j][7].ToString());
                EX.Parameters.Add("@NineColumnNameValue ", SqlDbType.VarChar, 8000, Value);



                i = Convert.ToInt32(EX.InvokeProcedure("proc_erp_Trn_Insert_into_Total_Drec", SqlTra, ValueDataType.Number, Con));


            }
        }
        return i;
    }
     public static bool CheckRows(Manage_Drec Description)
    {
        if (Description.InTable.Rows.Count > 0)
        {
            return true;

        }else
        {
            return false;
        }
    }
    #endregion

    # region "Procedures"
    public static void SetRadiobutton(string strStatus, RadioButton Rbyes, RadioButton rbNo)
    {
        if (strStatus == "Yes")
        {
            Rbyes.Checked = true;
        }
        else
        {
            Rbyes.Checked = false;
        }
        if (strStatus == "No")
        {
            rbNo.Checked = true;
        }
        else
        {
            rbNo.Checked = false;
        }
    }
    public static void Set_CheckBox(bool Status, CheckBox ChkName)
    {
        if (Status == true)
        {
            ChkName.Checked = true;
        }
        else
        {
            ChkName.Checked = false;
        }

    }
        public static void DoSomeFileWritingStuff(string message)
        {
            //Debug.WriteLine("Writing to file...");

            try
            {
                using (StreamWriter writer = new StreamWriter(LOG_FILE, true))
                {
                    if (message == "")
                    {
                        writer.WriteLine("Cache Callback: {0}", DateTime.Now);
                    }
                    else
                    {
                        writer.WriteLine(message);
                    }

                    writer.Close();
                }
                AspSendEmail smail = new AspSendEmail();
                //smail.strHost = mail.intrawebsolns.com
                smail.SendEmail("support@intrawebsolns.com", "amit4692@gmail.com", "Exception Error", message, "Error ERP");

            }
            catch (Exception x)
            {
                //Debug.WriteLine(x);
                DoSomeFileWritingStuff(x.Message);
            }

            //Debug.WriteLine("File write successful");
        }

    public static void SetRights(string Rights, Panel pnl)
    {
        foreach (Control ctrl in pnl.Controls)
        {
            if (ctrl.GetType().Name == "Button")
            {
                if ((((Button)ctrl).ID == "btnAdd" && Rights.IndexOf("A") > -1) || (((Button)ctrl).ID == "btnEdit" && Rights.IndexOf("E") > -1)
                    || (((Button)ctrl).ID == "btnDelete" && Rights.IndexOf("D") > -1))
                {
                    ctrl.Visible = true;
                }
                if ((((Button)ctrl).ID == "btnAdd" && Rights.IndexOf("S") > -1) || (((Button)ctrl).ID == "btnEdit" && Rights.IndexOf("S") > -1)
                        || (((Button)ctrl).ID == "btnDelete" && Rights.IndexOf("S") > -1))
                {
                    ctrl.Visible = true;
                }
            }

        }
    }
   #endregion

 public static void Enable_Btn_For_Add(Button btnAdd, Button btnCancel, Button btnDelete, Button btnEdit, Button btnUpdate, Button btnexit, Button btnfind)
    {
        btnEdit.Enabled = false;
        btnAdd.Enabled = false;
        btnfind.Enabled = false;
        btnUpdate.Enabled = true;
        btnCancel.Enabled = true;
        btnDelete.Enabled = false;
        btnexit.Enabled = true;



    }


    public static bool Check_Entry_exists(string tableName, string columnName, string columnValue)
    {
        bool result=false;
    try
    {
        ExecuteProcedures ex = new ExecuteProcedures(4, AccountCommonStrings.ConnectionString);
        ex.Parameters.Add("@tableName", SqlDbType.VarChar, 100, tableName);
        ex.Parameters.Add("@columnName", SqlDbType.VarChar, 100, columnName);
        ex.Parameters.Add("@columnValue", SqlDbType.VarChar, 100, @columnValue);
        ex.Parameters.Add("@isbitdeleted", SqlDbType.Int, 0);
        string temp =Convert.ToString( ex.InvokeProcedure("proc_check_Delete_entry", ValueDataType.String));
        if (temp == "false")
        {
            result = false;
        }
        else
        {
            result = true;
        }


    }
    catch (Exception)
    { 

    }
    return result;

    }

    public static bool Check_Entry_exists(string tableName, string columnName, string columnValue,string isbitdeleted)
    {
        bool result = false;
        try
        {
            ExecuteProcedures ex = new ExecuteProcedures(4, AccountCommonStrings.ConnectionString);
            ex.Parameters.Add("@tableName", SqlDbType.VarChar, 100, tableName);
            ex.Parameters.Add("@columnName", SqlDbType.VarChar, 100, columnName);
            ex.Parameters.Add("@columnValue", SqlDbType.VarChar, 100, @columnValue);
            ex.Parameters.Add("@isbitdeleted", SqlDbType.Int, isbitdeleted);
            string temp = Convert.ToString(ex.InvokeProcedure("proc_check_Delete_entry", ValueDataType.String));
            if (temp == "false")
            {
                result = false;
            }
            else
            {
                result = true;
            }


        }
        catch (Exception)
        {

        }
        return result;

    }

}