本文介绍了我的网格视图未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我添加一个没有出现在我的网格视图中的新行时。再次,当我正在调试时,它出现了。



Hi,
when i m adding a new row that is not appearing in my grid-view. Again when i am debugging it is appearing.

public partial class Default4 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con"]);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
            trlogin.Visible = false;
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        trlogin.Visible = true;
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //Create stringbuilder to store multiple DML statements 
        StringBuilder strSql = new StringBuilder(string.Empty);

        //Create sql connection and command
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con"]);
        SqlCommand cmd = new SqlCommand();

        //Loop through gridview rows to find checkbox 
        //and check whether it is checked or not 
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkUpdate = (CheckBox)
               GridView1.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkUpdate != null)
            {
                if (chkUpdate.Checked)
                {
                    // Get the values of textboxes using findControl
                    string strID = GridView1.Rows[i].Cells[1].Text;
                    string strName = ((TextBox)
                        GridView1.Rows[i].FindControl("Vendor_Name")).Text;

                    string strAsset_Type = ((TextBox)
                        GridView1.Rows[i].FindControl("Asset_Type")).Text;
                    string Status = ((TextBox)
                     GridView1.Rows[i].FindControl("Status")).Text;

                    string Date_Of_Registration = ((TextBox)
                     GridView1.Rows[i].FindControl("Date_Of_Registration")).Text;
                    string Phone_Number = ((TextBox)
                     GridView1.Rows[i].FindControl("Phone_Number")).Text;
                    string Email = ((TextBox)
                     GridView1.Rows[i].FindControl("Email")).Text;




                    try
                    {
                        string strUpdate = "UPDATE [VandorDetail] SET [Vendor_Name] = @Vendor_Name,  [Asset_Type] = @Asset_Type,[Status]=@Status,[Date_Of_Registration]=@Date_Of_Registration,[Phone_Number]=@Phone_Number,[Email]=@Email WHERE [ID] = @ID";
                        strSql.Append(strUpdate);

                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = strUpdate.ToString();
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@Vendor_Name", strName);
                        cmd.Parameters.AddWithValue("@Asset_Type", strAsset_Type);
                        cmd.Parameters.AddWithValue("@Status", Status);
                        cmd.Parameters.AddWithValue("@Date_Of_Registration", Date_Of_Registration);
                        cmd.Parameters.AddWithValue("@Phone_Number", Phone_Number);
                        cmd.Parameters.AddWithValue("@Email", Email);
                        cmd.Parameters.AddWithValue("@ID", strID);
                        cmd.Connection = con;
                        con.Open();
                        cmd.ExecuteNonQuery();
                        strSql.Append(strUpdate);
                    }


                    catch (SqlException ex)
                    {
                        string errorMsg = "Error in Updation";
                        errorMsg += ex.Message;
                        throw new Exception(errorMsg);
                    }
                    finally
                    {
                        con.Close();
                    }

                    UncheckAll();
                }
            }
        }
    }
   
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        StringCollection idCollection = new StringCollection();
        string strID = string.Empty;

        //Loop through GridView rows to find checked rows 
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkDelete = (CheckBox)GridView1.Rows[i].
                             Cells[0].FindControl("chkSelect");
            if (chkDelete != null)
            {
                if (chkDelete.Checked)
                {
                    strID = GridView1.Rows[i].Cells[1].Text;
                    idCollection.Add(strID);
                }
            }
        }
        if (idCollection.Count > 0)
        {
            //Call the method to Delete records 
            DeleteMultipleRecords(idCollection);

            // rebind the GridView
            GridView1.DataBind();
        }
        else
        {
            Label5.Text = "Please select any row to delete";
        }

    }
    private void DeleteMultipleRecords(StringCollection idCollection)
    {
        //Create sql Connection and Sql Command
        
        SqlCommand cmd = new SqlCommand();
        string IDs = "";

        foreach (string id in idCollection)
        {
            IDs += id.ToString() + ",";
        }

        try
        {
            string test = IDs.Substring
                          (0, IDs.LastIndexOf(","));
            string sql = "Delete from VandorDetail WHERE ID = '" + test + "'";
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            string errorMsg = "Error in Deletion";
            errorMsg += ex.Message;
            throw new Exception(errorMsg);
        }
        finally
        {
            con.Close();
        }
    }
    protected void chkSelect_CheckedChanged
                            (object sender, EventArgs e)
    {
        CheckBox chkTest = (CheckBox)sender;
        GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
        TextBox txtVname = (TextBox)grdRow.FindControl
                                            ("Vendor_Name");
        TextBox txtAType = (TextBox)grdRow.FindControl
                                          ("Asset_Type");
        TextBox txtStatus = (TextBox)grdRow.FindControl
                                            ("Status");
        TextBox txtDOR = (TextBox)grdRow.FindControl
                                          ("Date_Of_Registration");
        TextBox txtPNo = (TextBox)grdRow.FindControl
                                            ("Phone_Number");
        TextBox txtEmail = (TextBox)grdRow.FindControl
                                          ("Email");
        if (chkTest.Checked)
        {
            txtVname.ReadOnly = false;
            txtAType.ReadOnly = false;
            txtStatus.ReadOnly = false;
            txtDOR.ReadOnly = false;
            txtPNo.ReadOnly = false;
            txtEmail.ReadOnly = false;
        }
        else
        {
            txtVname.ReadOnly = true;
            txtAType.ReadOnly = true;
            txtStatus.ReadOnly = true;
            txtDOR.ReadOnly = true;
            txtPNo.ReadOnly = true;
            txtEmail.ReadOnly = true;
        }
    }
    private void UncheckAll()
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chkUncheck = (CheckBox)
                         row.FindControl("chkSelect");
            TextBox txtVname = (TextBox)row.FindControl
                                            ("Vendor_Name");
            TextBox txtAType = (TextBox)row.FindControl
                                              ("Asset_Type");
            TextBox txtStatus = (TextBox)row.FindControl
                                                ("Status");
            TextBox txtDOR = (TextBox)row.FindControl
                                              ("Date_Of_Registration");
            TextBox txtPNo = (TextBox)row.FindControl
                                                ("Phone_Number");
            TextBox txtEmail = (TextBox)row.FindControl
                                              ("Email");

            chkUncheck.Checked = false;
            txtVname.ReadOnly = true;
            txtAType.ReadOnly = true;
            txtStatus.ReadOnly = true;
            txtDOR.ReadOnly = true;
            txtPNo.ReadOnly = true;
            txtEmail.ReadOnly = true;
        }
    }


    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
      


        con.Open();

        string qryInsertProduct = "insert into VandorDetail(Vendor_Name,Asset_Type,Status,Date_Of_Registration,Phone_Number,Email) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox6.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
        SqlCommand comInsertProduct = new SqlCommand(qryInsertProduct, con);
        comInsertProduct.ExecuteNonQuery();
        
        GridView1.Visible = true;

        Populategridview();

        
           
           
    



    }

    protected void Populategridview()
    {

       
        string com = "Select * from VandorDetail";
        SqlCommand comm = new SqlCommand(com, con);
        comm.ExecuteNonQuery();
        con.Close();
        //GridView1.Visible = true;

    }


    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TextBox6.Text = Calendar1.SelectedDate.ToShortDateString();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        trlogin.Visible = false;
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}

推荐答案


这篇关于我的网格视图未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 00:14