Click here to Skip to main content
14,977,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to save Selected row in parent gridview how to select particular row and save in database can u help me any body
Posted

   
v2
Hi ,
This Example will guide you .
C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           SqlConnection con = new SqlConnection(@"Data Source=IT-DEV2\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
           string statment = "select item_code, Item_name, brand, size, section, price, Material, Qty, tax, tt  from Items ";
           SqlDataAdapter da = new SqlDataAdapter(statment, con);
           DataSet ds = new DataSet();
           da.Fill(ds);
           GridView1.DataSource = ds.Tables[0];
           GridView1.DataBind();
       }
   }
   protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
   {
       string  v1 = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
       string v2 = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
       string v3 = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
       string v4 = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
       SqlConnection con = new SqlConnection(@"Data Source=IT-DEV2\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
       con.Open();
       string statment = string.Format("insert into Items (Item_name, brand, size, section) values ('{0}' ,' {1}' , {2} , '{3}' )", v1,v2,Convert.ToInt32( v3),v4);
         SqlCommand cmd = new SqlCommand(statment,con);
       cmd.CommandType =  CommandType.Text;
       cmd.ExecuteNonQuery();
       cmd.Dispose();
       con.Close();

   }

ASP.NET
<div>
       <asp:GridView ID="GridView1" runat="server"
           onselectedindexchanging="GridView1_SelectedIndexChanging">
           <Columns>
               <asp:CommandField ShowSelectButton="True" />
           </Columns>
       </asp:GridView>
   </div>

Best Regards
M.Mitwalli
   

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