Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I hope you all are doing great.
I have a question regarding Update GridView using BoundField.
What i am doing currently is i have added a edit button in the GridView and once i click on it it gives me editable fields and update and cancel button with it.
Cancel and Edit buttons are working fine but i want update to work as well.

Below is my HTML Code:

<asp:GridView ID="gvSubmittedCases" runat="server" OnRowEditing="gvSubmittedCases_RowEditing" OnRowUpdating="gvSubmittedCases_RowUpdating"  
                    OnRowCancelingEdit="gvSubmittedCases_RowCancelingEdit"   CellPadding="4" GridLines="None" Width="100%" AutoGenerateColumns="False"  ForeColor="#333333" >  
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        <Columns>
                            <asp:BoundField DataField="SubmittedDate" ReadOnly="true" HeaderText="SubmittedDate" ItemStyle-Width="100" SortExpression="SubmittedBy" >
                            </asp:BoundField>
                            <asp:BoundField DataField="SubmittedBy" Visible="true" ReadOnly="true" HeaderText="Submitted By" ItemStyle-Width="150" SortExpression="Transaction Amount" >
                            </asp:BoundField>
                            <%--<asp:BoundField DataField="Display_Name" Visible="true" HeaderText="Display Name" ItemStyle-Width="150" SortExpression="Display Name" >
                            </asp:BoundField>--%>
                            <asp:BoundField DataField="CardNo" HeaderText="Card Number" ItemStyle-Width="100" SortExpression="CardNo" >
                            </asp:BoundField>
                            <asp:BoundField DataField="CardType" ReadOnly="true" HeaderText="Card Type" ItemStyle-Width="100" SortExpression="CardType" >
                            </asp:BoundField>
                            <asp:BoundField DataField="FraudType" HeaderText="Fraud Type" ItemStyle-Width="150" SortExpression="FraudType" >
                            </asp:BoundField>
                            <asp:BoundField DataField="CaseID" Visible="true" ReadOnly="true" HeaderText="CaseID" ItemStyle-Width="150" SortExpression="CaseID" >
                            </asp:BoundField>
                            <asp:BoundField DataField="Region"  Visible="true" HeaderText="Region" ItemStyle-Width="150" SortExpression="Region" >
                            </asp:BoundField>
                            <asp:BoundField DataField="AuthorisationCode"  Visible="true" ReadOnly="true" HeaderText="Auth Code" ItemStyle-Width="150" SortExpression="Auth Code" >
                            </asp:BoundField>
                            <asp:BoundField DataField="TransactionDate" Visible="true" HeaderText="Transaction Date" ItemStyle-Width="150" SortExpression="TransactionDate" >
                            </asp:BoundField>
                            <asp:BoundField DataField="TransactionAmount" Visible="TRUE" HeaderText="Transaction Amount" ItemStyle-Width="150" SortExpression="Transaction Amount" >
                            </asp:BoundField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
                                    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
                                    </EditItemTemplate>
                                </asp:TemplateField>
                           
                            </Columns>



And this is Code Behind.


private void CaseView()
  {
      BusinessLayer k = new BusinessLayer();
      DataTable dt = k.ViewCases(UserID,UserType);
      if (dt.Rows.Count >= 0)
      {
          gvSubmittedCases.DataSource = dt;
          gvSubmittedCases.DataBind();
      }
      else
          lblCases.Text = "No Cases Found";
  }

  protected void gvSubmittedCases_RowEditing(object sender, GridViewEditEventArgs e)
  {
      gvSubmittedCases.EditIndex = e.NewEditIndex;
      CaseView();
  }


protected void gvSubmittedCases_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
   {
       gvSubmittedCases.EditIndex = -1;
       CaseView();

   }



Also, there is one field FraudType that i want to be dropDown when the user clicks on edit.

I hope i have cleared my question.
You can ask if anything needs to get cleared.

Note:

I am using Stored Procedures to Update the database, which is in SQL


Thanks

What I have tried:

I have searched web, but there is no proper solution for BoundField + Stored Procedures
Posted
Updated 27-Jan-17 3:05am
Comments
Suvabrata Roy 27-Jan-17 4:34am    
Please share the update button's code behind
Faran Saleem 27-Jan-17 4:38am    
I have not written any code behind for the update button
Suvabrata Roy 27-Jan-17 4:47am    
Please go through this link : http://www.c-sharpcorner.com/UploadFile/1e050f/edit-and-update-record-in-gridview-in-Asp-Net/
Faran Saleem 27-Jan-17 5:03am    
I have already checked that. But it does not use BoundField. Can you please help me in that?

1 solution

The RowUpdating event passes in a GridViewUpdateEventArgs instance[^].

This contains three dictionaries you can use to read the values:
  • Keys[^] contains the key values for the row, as specified by the DataKeyNames property on the grid.
  • NewValues[^] contains the new values submitted from the row.
  • OldValues[^] contains the original values from the row, in case you need to check which fields have changed.

So, to extract the values which might have changed:
C#
protected void gvSubmittedCases_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string cardNo = Convert.ToString(e.NewValues["CardNo"]);
    string fraudType = Convert.ToString(e.NewValues["FraudType"]);
    string region = Convert.ToString(e.NewValues["Region"]);
    DateTime transactionDate = Convert.ToDateTime(e.NewValues["TransactionDate"]);
    decimal transactionAmount = Convert.ToDecimal(e.NewValues["TransactionAmount"]);
    ...
    
    gvSubmittedCases.EditIndex = -1;
    CaseView();
}

You'll want to set the grid's DataKeyNames to the name of the primary key, and extract that from the e.Keys dictionary.

You should then be able to call your stored procedure to update the record.

To change the "fraud type" column to a drop-down list, you'll need to use a TemplateField:
Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control[^]
 
Share this answer
 
Comments
Faran Saleem 3-Feb-17 0:54am    
Thank you. With just a little addition the code worked.
Thanks a ton for your help..:)

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