Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two gridviews one is connected to one table and the the other is connected to other table both in the same database



when a record in gridview1 is selected using the gridview select tool then by clicking a button it will move the record to table2 and now shown on gridview 2
iv'e tried

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        GridViewRow r = GridView1.SelectedRow;
        string strQuery = "insert into table values('" + r.Cells[0] + "','" + r.Cells[1] + "')";

but i need it connected to button1

You could store the current selected row as a class variable, then reference it when button is clicked

public GridViewRow r = null;

//row seleccted
r = GridView1.SelectedRow;

//button clicked
if(r!=null){/*do something*/}
Share this answer
protected void btnSend_Click(object sender, EventArgs e)
        DataTable dt = new DataTable();

        foreach (GridViewRow grdRow in gridview1.Rows) ////////////// Add new Data from GridMenuList TO Datatable DT
                if (((CheckBox)grdRow.FindControl("chkSelect")).Checked)
                    DataRow drow = dt.NewRow();

                    Label lblMenuName = (Label)grdRow.FindControl("lblMenuName");

                    HiddenField hdnID = (HiddenField)grdRow.FindControl("hdnID");

                    drow["MenuName"] = lblMenuName.Text;               
                    drow["MenuID"] = hdnID.Value;
        if (dt.Rows.Count > 0)
            gridview2.DataSource = dt;

protected void gridview2_RowDataBound(object sender, GridViewRowEventArgs e)

        if (e.Row.RowType == DataControlRowType.DataRow)
            DataRowView dr = (DataRowView)e.Row.DataItem;
            //e.Row.Cells[0].Text = dr["MenuName"].ToString();
            Label lblMenuName = e.Row.FindControl("lblMenuName") as Label;
            lblMenuName.Text = dr["MenuName"].ToString();

            HiddenField hdnID = e.Row.FindControl("hdnID") as HiddenField;
            hdnID.Value = dr["MenuID"].ToString();
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