Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
protected void gvMain_RowEditing(object sender, GridViewEditEventArgs e)
    {
           }


I have even handlar as above which is empty

on row_Command I am getting data and assign it to textboxes presen outside of gridview

Now I want to update that data is is updating but when I Load Gridview whatever row updated is showing in edit mode but I dont want it in update mode

What I have tried:

<div class="row table-responsive grid" style="margin-top: 20px; margin: 1px">
                                              <asp:GridView ID="gvMain" runat="server" HeaderStyle-BackColor="#336699" HeaderStyle-ForeColor="White" Font-Size="15px" PageSize="15"
                           AutoGenerateColumns="false" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Width="100%" OnRowDataBound="gvMain_RowDataBound"
                           OnRowCommand="gvMain_RowCommand" OnRowEditing="gvMain_RowEditing" OnRowDeleting="gvMain_RowDeleting">
                           <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                           <Columns>
                               <asp:BoundField DataField="Master_Ref_Value_Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide" />
                               <asp:BoundField DataField="Main_Test_Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide" />
                               <asp:BoundField HeaderText="Sr.No">
                                   <FooterStyle BackColor="#CCCCCC" />
                                   <HeaderStyle Height="30px" HorizontalAlign="Right"></HeaderStyle>
                                   <ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
                               </asp:BoundField>
                               <asp:BoundField HeaderText="Ref Name" DataField="Ref_Name" HeaderStyle-HorizontalAlign="Center">
                                   <ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
                               </asp:BoundField>
                               <asp:BoundField HeaderText="KeyVal" DataField="KeyVal" HeaderStyle-HorizontalAlign="Center">
                                   <ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
                               </asp:BoundField>
                               <asp:BoundField HeaderText="Unit" DataField="Unit" HeaderStyle-HorizontalAlign="Center">
                                   <ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
                               </asp:BoundField>
                               <asp:BoundField HeaderText="Value" DataField="Value" HeaderStyle-HorizontalAlign="Center">
                                   <ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
                               </asp:BoundField>
                               <asp:BoundField HeaderText="Main_Test" DataField="Main_Test" HeaderStyle-HorizontalAlign="Center">
                                   <ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
                               </asp:BoundField>


                               <asp:ButtonField ButtonType="Link" CausesValidation="false" HeaderText="Edit" ControlStyle-ForeColor="blue" CommandName="Edit" Text="Edit">
                                   <HeaderStyle Width="30" />
                               </asp:ButtonField>
                               <asp:ButtonField ButtonType="Link" CausesValidation="false" HeaderText="Delete" ControlStyle-ForeColor="blue" CommandName="Delete" Text="Delete">
                                   <HeaderStyle Width="30" />
                               </asp:ButtonField>

                           </Columns>
                       </asp:GridView>


                   </div>



btn update outside of gridview

<div class="col-md-1">
                                            <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btn btn-success" ValidationGroup="submit" OnClick="btnSubmit_Click" />

                                            <asp:Button ID="btnUpdate" runat="server" Text="Update" CssClass="btn btn-success" ValidationGroup="submit" OnClick="btnUpdate_Click" />
                                        </div>


protected void gvMain_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
   }




protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int MasterRefValueId = 0;
        int MainTestId = 0;

        int.TryParse(ddlMainTest.SelectedValue.ToString(), out MainTestId);

        if (hdnMasterRefValueId.Value != null)
        {
            int.TryParse(hdnMasterRefValueId.Value.ToString(), out MasterRefValueId);
            hdnMasterRefValueId.Value = null;
        }
        Master_ref_value objMaster_ref_value = new Master_ref_value(MasterRefValueId, MainTestId, txtRefName.Text, txtKeyVal.Text, txtUnit.Text, txtValue.Text);

        bool result;
        result = objMaster_ref_value.Update();

        if (result)
        {
            lblMessage.Text = "Record Updated Successfully";
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Visible = true;
            Reset();
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "HideLabel();", true);
            //gvMain.EditIndex = e.RowIndex - 1;
        }
        else
        {
            lblMessage.Text = "Record Updation failed";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Visible = true;
            Reset();
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "HideLabel();", true);
        }
        Panel4.Visible = false;
        fillgridview();
        lblMessage.Visible = false;
        EmpRegistrationpanel2.Visible = false;
        EmpRegistrationpanel1.Visible = true;
    }




where i should use
//gvMain.EditIndex = e.RowIndex - 1;
Posted
Updated 9-Nov-18 5:33am
Comments
Vincent Maverick Durano 6-Nov-18 16:42pm    
I'm not sure I follow you correctly. Can you please elaborate?
Herman<T>.Instance 9-Nov-18 7:05am    
What he seems to want is when you click row 0 that row -1 gets set in EditMode.
Vincent Maverick Durano 9-Nov-18 11:34am    
I've read again the question and I think that's not the case.

1 solution

The default behavior of GridView control is to perform edit and update one row at time. It seems like you are trying to use an Update Button outside your GridView which there's no way for you to identify which row you are actually editing. To update a row, you should be handling the RowUpdating event to do that and then handle your update logic there instead.

Here are a couple of examples:
Using TemplateFields
Adding Rows in GridView with Edit, Update and Delete Functionality[^]
Using BoundFields
GridView Insert, Edit, Update and Delete – The Ado.Net way[^]


Now, if you are trying to select a row from GridView and populate the selected values in your TextBox that is outside your GridView then you can get rid of the RowEditing event and Edit ButtonField. You would simply need to setup the ShowSelectButton CommandField in the GridView like:

ASP.NET
<asp:GridView ID="GridView1"  runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"  >
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField   />
<asp:BoundField   />
</Columns>
</asp:GridView>


and at SelectedIndexChanged event you can do something like this to get the selected row data and populate your TextBox:

C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){
       //Where index of Cells is the column in Grid that you wan't to refer
       TextBox1.Text = GridView1.SelectedRow.Cells[0].Text; 
       TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
       //If accessing TemplateField column then you can use FindControl method
} 


Then you can use the code that what you have in your Update Button that is outside the GridView to update the data.
 
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