Click here to Skip to main content
15,919,178 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
How we show data of grid view on page if we click on view link or button of that gridview
Posted

1 solution

Take RowCommand command Property of data grid view. and write bellow code in code behind when you click on view it go to row command and check for command name here i have command name Edit Record for update I assign the selected row value to textboxes you can assign that values to label also.


protected void grdvwParts_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           try
           {


               int id = Convert.ToInt32(e.CommandArgument);

               if (e.CommandName.Equals("EditRecord"))                               //onclick edit link button of grigview
               {
                   GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                   string partCode = row.Cells[1].Text;
                   string partname = row.Cells[2].Text;
                   string section = row.Cells[3].Text;
                   tbcPrtCode.Text = partCode;
                   tbxPrtNm.Text = partname;
                   ddlsection.SelectedItem.Text = section;

               }
               if (e.CommandName.Equals("DeleteRecord"))                               //onclick remove link button of grigview
               {
                   // ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert(' Are you sure')", true);
                   con = new SqlConnection(strConnString);
                   cmd = new SqlCommand(String.Format("DELETE FROM Parts WHERE(prt_id ='" + id + "')"), con);
                   cmd.Connection = con;
                   con.Open();
                   int result = cmd.ExecuteNonQuery();
                   con.Close();


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


hope it will help you
 
Share this answer
 
v2
Comments
Richard Deeming 21-Oct-15 8:59am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
asplover1 22-Oct-15 2:30am    
Thanx for your suggestion. I will apply it.Thank you.
Member 12300401 5-Feb-16 5:18am    
grid.Column(columnName: "Action", format: (item) => Html.ActionLink("Manage Goals", "Managaekrasgoals", "ManageKrasGoals"))


but unable to get the selected rows value in the controler
Member 12300401 5-Feb-16 5:18am    
grid.Column(columnName: "Action", format: (item) => Html.ActionLink("Manage Goals", "Managaekrasgoals", "ManageKrasGoals"))


but unable to get the selected rows value in the controler
Member 12300401 5-Feb-16 4:52am    
grid.Column(columnName: "Action", format: (item) => Html.ActionLink("Manage Goals", "Managaekrasgoals", "ManageKrasGoals"))


but unable to get the selected rows value in the controler

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