Skip to content

Selected record in gridview move to another table?

shiva kumar

Asked Apr 7, 2012, 8:43 AM EDT2 answers
Share
LinkedInX
Hi exterts,

   my req is in my gridview i have some records and each record having checkbox. if i want to select 1 checkbox and click on button that record move into another table.......

give me reply..

bye

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    0
    Hi,

    You need to iterate the grid after selecting some rows in the main grid.

    If the items are selected then it should be identified and added those results into the datatable.

            DataTable dt = new DataTable();
            DataColumn dColumn;

            dColumn = new DataColumn("ID");
            dt.Columns.Add(dColumn);

            dColumn = new DataColumn("Name");
            dt.Columns.Add(dColumn);

            dColumn = new DataColumn("Address");
            dt.Columns.Add(dColumn);

            dColumn = new DataColumn("City");
            dt.Columns.Add(dColumn);

            DataRow dRow;

            for (int i = 0; i < gvMainGrid.Rows.Count; i++)
            { 
                GridViewRow gRow = gvMainGrid.Rows[i];
                CheckBox chkItem = (CheckBox)gRow.FindControl("chkItem");
                if (chkItem.Checked)
                {
                    Label lblID = (Label)gRow.FindControl("lblID");
                    Label lblName = (Label)gRow.FindControl("lblName");
                    Label lblAddress = (Label)gRow.FindControl("lblAddress");
                    Label lblCity = (Label)gRow.FindControl("lblCity");

                    dRow = dt.NewRow();
                    dRow["ID"] = lblID.Text.Trim();
                    dRow["Name"] = lblName.Text.Trim();
                    dRow["Address"] = lblAddress.Text.Trim();
                    dRow["City"] = lblCity.Text.Trim();

                    dt.Rows.Add(gRow);
                }
            }

    After adding all the selected rows, you need to bind it into another gridview control.

    gvSubGrid.DataSource = dt;
    gvSubGrid.DataBind();

    Hope this help you.

    0