Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ID-------Name--------Quantity---------Status--------Button
-------------------------------------------------------------------
1----------S4--------------3-------------Pending-------Update
1----------S3--------------5-------------Pending-------Update


This is a sample gridview in my project. I want to update "Status" from "Pending to Delivered" everytime I click the button "Update".

Thanks for helping.

C#
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
       AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999"
       BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2"
       DataSourceID="SqlDataSource1"
       EmptyDataText="There are no data records to display." ForeColor="Black"
       Width="724px" onrowcommand="GridView1_RowCommand">
       <Columns>
           <asp:BoundField DataField="Email_Address" HeaderText="Email_Address"
               SortExpression="Email_Address" />
           <asp:BoundField DataField="Order_Number" HeaderText="Order #"
               SortExpression="Order_Number" />
           <asp:BoundField DataField="Item_Purchased_Number"
               HeaderText="Purchased #" SortExpression="Item_Purchased_Number" />
           <asp:BoundField DataField="Branch" HeaderText="Branch"
               SortExpression="Branch" />
           <asp:BoundField DataField="Date_Issued" HeaderText="Date_Issued"
               SortExpression="Date_Issued" />
           <asp:BoundField DataField="Status" HeaderText="Status"
               SortExpression="Status" />
           <asp:BoundField DataField="Remarks" HeaderText="Remarks"
               SortExpression="Remarks" />
          <asp:TemplateField HeaderText="View More">
       <ItemTemplate>
           <asp:Button ID="btnUpdate"
           CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
           " CommandName="Delivered" runat="server" Text="Update" />
       </ItemTemplate>
       </asp:TemplateField>
       </Columns>
       <FooterStyle BackColor="#CCCCCC" />
       <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
       <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
       <RowStyle BackColor="White" />
       <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
       <SortedAscendingCellStyle BackColor="#F1F1F1" />
       <SortedAscendingHeaderStyle BackColor="#808080" />
       <SortedDescendingCellStyle BackColor="#CAC9C9" />
       <SortedDescendingHeaderStyle BackColor="#383838" />
   </asp:GridView>
   <asp:SqlDataSource ID="SqlDataSource1" runat="server"
       ConnectionString="<%$ ConnectionStrings:CellzoneDatafilesConnectionString1 %>"

       SelectCommand="SELECT * FROM [Customer_Remarks] WHERE (([Branch] = @Branch) AND ([Status] = @Status))">
       <SelectParameters>
           <asp:Parameter DefaultValue="SM Dasmariñas" Name="Branch" Type="String" />
           <asp:Parameter DefaultValue="Pending" Name="Status" Type="String" />
       </SelectParameters>
   </asp:SqlDataSource>




C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

    if (e.CommandName == "Delivered")
    {
        int index = Convert.ToInt32(e.CommandArgument.ToString());
    string stat =  GridView1.Rows[index].FindControl("Status").ToString();
        stat = "Delivered";
    }
}
Posted
Updated 17-Mar-14 3:20am
v2
Comments
CHill60 17-Mar-14 8:23am    
What have you tried?
DLSU-D Student 17-Mar-14 8:25am    
I tried a edit button but I want is everytime I click a button from a gridview it update the Status. BTW. Data is coming from Database.
CHill60 17-Mar-14 8:33am    
I meant for you to post the code that you had already tried so that we could help you find the problem. However Solution 1 has now been posted. If that doesn't work first time for you make sure you also have onrowcommand="grdSearch_RowCommand" in your HTML.

In grdview_RowCommand you can change the status of the row...

C#
protected void grdSearch_RowCommand(object sender, GridViewCommandEventArgs e)
        {
e.Row.FindControl("Status").Text="Delivered";

}
 
Share this answer
 
v2
Comments
DLSU-D Student 17-Mar-14 8:33am    
System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'findControl' and no extension method 'findControl' accepting a first argument of type 'System.Web.UI.WebControls.GridViewCommandEventArgs' could be found (are you missing a using directive or an assembly reference?)


Sir I get an error.

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999"
BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." ForeColor="Black"
Width="724px">
<columns> <asp:BoundField DataField="Email_Address" HeaderText="Email_Address"
SortExpression="Email_Address" />
<asp:BoundField DataField="Order_Number" HeaderText="Order #"
SortExpression="Order_Number" />
<asp:BoundField DataField="Item_Purchased_Number"
HeaderText="Purchased #" SortExpression="Item_Purchased_Number" />
<asp:BoundField DataField="Branch" HeaderText="Branch"
SortExpression="Branch" />
<asp:BoundField DataField="Date_Issued" HeaderText="Date_Issued"
SortExpression="Date_Issued" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
<asp:BoundField DataField="Remarks" HeaderText="Remarks"
SortExpression="Remarks" />
<asp:CommandField ButtonType="Button" HeaderText="Update" ShowHeader="True"
ShowSelectButton="True" />
<footerstyle backcolor="#CCCCCC">
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<rowstyle backcolor="White">
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<sortedascendingcellstyle backcolor="#F1F1F1">
<sortedascendingheaderstyle backcolor="#808080">
<sorteddescendingcellstyle backcolor="#CAC9C9">
<sorteddescendingheaderstyle backcolor="#383838">


This is my gridview.


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
e.findControl("Status").innerText = "Delivered";
}

This is in .cs
Well I can't explain the entire thing. So take a look at here :
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^], which will explain how to insert, edit, update and delete data in gridview using asp.net.

-KR
 
Share this answer
 
Add OnRowDataBound=Gridview1_RowDataBound() in Grid view,

And Add the same method in Code behind..
 
Share this answer
 
Comments
DLSU-D Student 17-Mar-14 9:09am    
'System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.Web.UI.WebControls.GridViewCommandEventArgs' could be found (are you missing a using directive or an assembly reference?)

I get error again. It's say does not contain definition for 'Row'
DLSU-D Student 17-Mar-14 9:10am    
'ASP.adminsmdasma_pending_aspx' does not contain a definition for 'Gridview1_RowDataBound' and no extension method 'Gridview1_RowDataBound' accepting a first argument of type 'ASP.adminsmdasma_pending_aspx' could be found (are you missing a using directive or an assembly reference?)

This is another 1 bro.
Modify the code like below...
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Delivered")
    {
        int index = Convert.ToInt32(e.CommandArgument.ToString());
        GridViewRow row = GridView.Rows[index];
        (row.FindControl("Status")).Text = "Delivered";
    }
}
 
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