Click here to Skip to main content
15,877,246 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using gridview for display detail, rowcommand event for edit and delete rows. in code behind, during step debugging code is working properly and deletes values from database. but it doesnot reflect to user.

What I have tried:

//gridview code in aspx page
ASP.NET
<asp:GridView ID="GridDetails" runat="server" CssClass="GridviewSearch" BackColor="White"BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" TabIndex="11"
CellPadding="3" ForeColor="Black" GridLines="Vertical" OnRowCommand="GridDetails_RowCommand"                                      OnRowDataBound="GridDetails_RowDataBound" ><Columns>
<asp:TemplateField HeaderText="Action" >
<ItemTemplate><asp:LinkButton ID="LBtnEdit" runat="server" ForeColor="#FF944D" CommandName="Edit"
Text="Edit" CausesValidation="false" CssClass="gridCommand"></asp:LinkButton>
<asp:LinkButton ID="LBtnDelete" runat="server" ForeColor="#FF944D" CommandName="Delete"
Text="Delete" CausesValidation="false" CssClass="gridCommand"></asp:LinkButton>
</ItemTemplate></asp:TemplateField>
</Columns><FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle ForeColor="White" BackColor="Black" Font-Bold="True"/>
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>



//code in .cs page


C#
protected void GridDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
            int RowIndex = row.RowIndex;

            IDofSelectedItem.Value = row.Cells[1].Text;

            if (e.CommandName == "Edit")
            {
                PnlAddTask.Visible = true;
                if ((TxtTaskDetail.Text.Trim() != "") || (TxtCompletedOn.Text.Trim() != ""))
                {
                    TxtTaskDetail.Focus();
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('There is already some data. So, first save it.')", true);
                    return;
                }

                BtnSubmit.Text = "Update";
                TxtTaskDetail.Text = row.Cells[2].Text;
                TxtCompletedOn.Text = row.Cells[3].Text;
                //TxtTaskDetail.Focus();
            }

            if (e.CommandName == "Delete")
            {
                if (con.State == ConnectionState.Closed)
                { con.Open(); }

                IsInTransaction = true;
                trans = con.BeginTransaction();
                cmd = new SqlCommand("Delete from TblTaskManager where TaskID = " + Convert.ToInt16(IDofSelectedItem.Value) + " ", con, trans);
                cmd.ExecuteNonQuery();
                trans.Commit();
                IsInTransaction = false;
                con.Close();

                ChangeLinkButtonText();
                if (ClickOn == "UpComming")
                {
                    LBtnUpComming_Click(sender, e);
                }
                else if (ClickOn == "Pending")
                {
                    LBtnPending_Click(sender, e);
                }
                else if (ClickOn == "Completed")
                {
                    LBtnCompleted_Click(sender, e);
                }

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "alert('Delete Successfully.');", true);
            }
    }



C#
protected void LBtnUpComming_Click(object sender, EventArgs e)
    {
            LBtnUpComming.ForeColor = System.Drawing.Color.Red;
            LBtnPending.ForeColor = System.Drawing.Color.Blue;
            LBtnCompleted.ForeColor = System.Drawing.Color.Blue;

            MyFunction.FillGridView("Select TaskID, Task As 'Task Detail', CONVERT(varchar(20),CompletedOn, 106) As 'Completed On' from TblTaskManager Where EmployeeID = " + ClsCommonVariable.LoginID + " and CompletedOn >= '" + DateTime.Now.Date + "' and Completed = 'False' Order by CompletedOn Asc", ref GridDetails);
            if (GridDetails.Rows.Count == 0)
            {
                PnlForGrid.Visible = false;
            }
            else
            {
                PnlForGrid.Visible = true;
            }
    }



C#
void ChangeLinkButtonText()
    {
        if (con.State == ConnectionState.Closed)
        { con.Open(); }

        cmd = new SqlCommand("Select Count(*) from TblTaskManager Where EmployeeID = " + ClsCommonVariable.LoginID + " and CompletedOn >= '" + DateTime.Now.Date + "' and Completed = 'False'", con);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            LBtnUpComming.Text = "UpComming (" + dr[0].ToString() + ")";
        }
        else
        {
            LBtnUpComming.Text = "UpComming";
        }
        dr.Close();
        con.Close();
    }
Posted
Updated 24-Nov-16 2:25am

1 solution

If changes are not reflecting on front end while have been done in DB then you have to set datasource and then call databind() again in the row_command event

GridDetails.DataSource = ...;
GridDetails.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900