Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
i m working to asp.net i need to perform one task when i m selecting gridview one row then all value come into respective textbox in form so i need to edit or delete then save it in DB.
Posted
Updated 13-Mar-12 0:58am
v2

Refer this code, I have used it in my project:
C#
private void dgvTpTransferSearchResult_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtName.Text = Convert.ToString(dgvTpTransferSearchResult.SelectedRows[0].Cells["TPTransferName"].Value);
            txtAddress.Text = Convert.ToString(dgvTpTransferSearchResult.SelectedRows[0].Cells["Address"].Value);
            txtTelephone.Text = Convert.ToString(dgvTpTransferSearchResult.SelectedRows[0].Cells["Telephone"].Value);
            txtMobile.Text = Convert.ToString(dgvTpTransferSearchResult.SelectedRows[0].Cells["Mobile"].Value);
            txtEmail.Text = Convert.ToString(dgvTpTransferSearchResult.SelectedRows[0].Cells["Email"].Value);
            txtName.Enabled = txtAddress.Enabled = txtMobile.Enabled = txtTelephone.Enabled = txtEmail.Enabled = true;
        }

Hope it helps..!
 
Share this answer
 
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
 
    }
 

private void BindData()
    {
        string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=database_name;Integrated Security=True";
        string query = "SELECT 1st_column,2nd_column,3rd_column FROM Table_Name";
        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();
 
    }

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
 
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        BindData();
    }
 
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        
        TextBox txt1st_column = (TextBox)row.FindControl("txt1st_column");	
        TextBox txt2nd_column = (TextBox)row.FindControl("txt2nd_column");
        TextBox txt3rd_column = (TextBox)row.FindControl("txt3rd_column");   

        int a = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string b = txt2nd_column.Text;
        string c = txt3rd_column.Text;
 
        UpdateProduct(a, b,c);
 
    }
 
private void UpdateProduct(int a, string b,string c)
    {
        try
        {
            string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=Database;Integrated Security=True";
            string query = "UPDATE Table_Name SET 2nd_column = @b, 3rd_column = @c WHERE 1st_column = @a";
 

            SqlConnection con = new SqlConnection(constr);
            SqlCommand com = new SqlCommand(query, con);
 
            com.Parameters.Add("@a", SqlDbType.Int).Value = 1st_column;
            com.Parameters.Add("@b", SqlDbType.NVarChar).Value = 2nd_column;
            com.Parameters.Add("@c", SqlDbType.NVarChar).Value = 3rd_column;
 

            con.Open();
            com.ExecuteNonQuery();
            con.Close();
 

            GridView1.EditIndex = -1;
            BindData();
        }
        catch (Exception ex)
        {
            throw ex;
    }
 
protected void btn_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
	try
	{
	string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=Database;Integrated Security=True";
	string query = "DELETE FROM Table_Name WHERE 1st_column=@a";
 
	SqlConnection con = new SqlConnection(constr);
	SqlCommand cmd = new SqlCommand();
 
	cmd.Parameters.Add("@a", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text)";
	cmd.Connection = con;
 
	con.Open();
	cmd.ExecuteNonQuery();
 
	con.Close();
	BindData();
	}
	catch(Exception ex)
	{
	    throw ex;
	}
  }
 
Share this answer
 
v2

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