Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I take one gridview in which there are two link buttons (for edit and delete).
I used a rowCommand property for update and delete. For deletion it works properly .
For update I want values of of row whose edit button is clicked. I tried many ways.
But I didnt get it.
Can anybody help me plz.

hereis my code.....

C#
protected void grdvwParts_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          try
          {
              int id = Convert.ToInt32(e.CommandArgument);
              if (e.CommandName.Equals("EditRecord"))
              {
              //    LinkButton lnkbtn = sender as LinkButton;
              //    //getting particular row linkbutton
              //    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
              //    //getting userid of particular row
              //    int userid = Convert.ToInt32(grdvwParts.DataKeys[gvrow.RowIndex].Value.ToString());

              //    //int coed=grdvwParts.Rows(id).cell(0).text;
              //    //GridViewRow dv = ((LinkButton)sender).Parent.Parent as GridViewRow;
              //    string part = grdvwParts.DataKeys[id].Value.ToString().Trim();
              //    string code = Convert.ToString(DataBinder.Eval("Part code", "prt_id"));
              //    string section = Convert.ToString(DataBinder.Eval("Installation/ Production", "sdesc"));
              //    tbcPrtCode.Text = code;
              //    tbxPrtNm.Text = part;
                  //if (e.Row.RowType == DataControlRowType.DataRow)
                 // {
                      tbcPrtCode.Text = e.Row.Cells[0].Text;
                 // }
              }
              if (e.CommandName.Equals("DeleteRecord"))
              {
                  ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert(' Are')", true);
                  con = new SqlConnection(strConnString);
                  cmd = new SqlCommand(String.Format("DELETE FROM Part WHERE(prt_id =" + id + ")"), con);
                  con.Open();
                  cmd.ExecuteNonQuery();
                  con.Close();
                  BindGridView();

              }
          }
          catch (Exception ex)
          {
              throw ex;
          }
          finally
          {
              if (con != null)
              {
                  con.Close();
                  con.Dispose();
              }
          }

      }



****************
and my aspx file is
XML
<div class="table-responsive">
                       <<asp:GridView ID="grdvwParts" runat="server" AllowPaging="True"
                             AllowSorting="True" CssClass="table table-bordered" AutoGenerateColumns="False"
                             CellPadding="0"  GridLines="None"  Width="100%" ShowFooter="True"
                             onrowcommand="grdvwParts_RowCommand"
                             onselectedindexchanged="grdvwParts_SelectedIndexChanged" >
          <Columns>
          <asp:TemplateField HeaderText="Sr.No." SortExpression="Sr.No." ItemStyle-ForeColor="black">
                 <ItemTemplate>
                       <asp:Label ID="SrNo" runat="server" Text='<%# ((GridViewRow) Container).RowIndex+1 %>'></asp:Label>
                  </ItemTemplate>
          </asp:TemplateField>
                 <asp:BoundField DataField = "prt_id" HeaderText = "Part code" />
                 <asp:BoundField DataField = "prt_nm" HeaderText = "Parts" />
                 <asp:BoundField DataField = "sdesc" HeaderText = "Installation/ Production" />
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                    <asp:LinkButton runat="server" ID="lbtnEdit" Text="Edit" CommandArgument='<%# Eval("prt_id")%>' CommandName="EditRecord"></asp:LinkButton>
                  <%--
                    <a target='_blank' href='Test.aspx?code={0}'>
                        <asp:Label ID="lblEdit" runat="server" Text = "Edit" CommandArgument='<%# Eval("ID")%>' CommandName="EditRecord"></asp:Label>
                        </a>--%>
                        </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                    <asp:LinkButton runat="server" ID="lbtnRemove" Text="Remove" CommandArgument='<%# Eval("prt_id")%>' CommandName="DeleteRecord"></asp:LinkButton>
                  <%--  <a target='_blank' href='Test.aspx?code={0}'>
                        <asp:Label ID="lblremove" runat="server" Text = "remove" CommandArgument='<%# Eval("ID")%>' CommandName="DeleteRecord"></asp:Label>
                        </a>--%>
                        </ItemTemplate>

                </asp:TemplateField>
               </Columns>
         </asp:GridView>
                  </div>
Posted
Updated 9-Oct-15 0:24am
v2
Comments

Try below code:
C#
protected void grdvwParts_RowCommand(object sender, GridViewCommandEventArgs e)
{
	  try
	  {
		  int id = Convert.ToInt32(e.CommandArgument);
		  if (e.CommandName.Equals("EditRecord"))
		  {
			string CustomerID = ((Label)GridView1.Rows[e.RowIndex]
								.FindControl("lblCustomerID")).Text;
			string Name = ((TextBox)GridView1.Rows[e.RowIndex]
								.FindControl("txtContactName")).Text;
			string Company = ((TextBox)GridView1.Rows[e.RowIndex]
								.FindControl("txtCompany")).Text;
			SqlConnection con = new SqlConnection(strConnString);
			SqlCommand cmd = new SqlCommand();
			cmd.CommandType = CommandType.Text;
			cmd.CommandText = "update customers set ContactName=@ContactName," +
			 "CompanyName=@CompanyName where CustomerID=@CustomerID;" +
			 "select CustomerID,ContactName,CompanyName from customers";
			// Update your data
		}
	}
}

Here it is fetching value from lblCustomerID and others - so you need to change as per your page control ID.
 
Share this answer
 
v2
Comments
asplover1 9-Oct-15 6:20am    
Thanx For reply.
In gridview are u used a template field
asplover1 9-Oct-15 6:21am    
I used BoundField then how can I get that row's value
[no name] 9-Oct-15 6:29am    
Try with below code:
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gv_FilesList.Rows[index];
string partCode = row.Cells[1].Text;

You may need to change Cells index valus. Here it is 1 - change as per your need. Check whether you are getting value.

Secondly don't put aspx code as answer. Better improve your question by adding aspx code.
asplover1 9-Oct-15 6:35am    
Thanx for suggestion. Bt posted by mistake.
[no name] 9-Oct-15 6:36am    
Is that worked?

Please delete the second answer which you put as answer.

Thanks!
Try like below...
C#
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

var something = row.Cells[0].Text;
 
Share this answer
 
Comments
asplover1 11-Oct-15 23:57pm    
Thank you very much. It is very helpful
Most welcome buddy. :)

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