Click here to Skip to main content
15,881,831 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I have a dataset which binds data to the Gridview in my page. I have check boxes to it. Whe i click a button in my page i need to bind the selected rows in my gridview into another dataset or into another datatable.pls help how to do it.. i have attached my code with this...

pls help with necessary changes in my code...
C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           fillgrid();
       }
   }

   protected void btnPdfReport_Click(object sender, EventArgs e)
   {
        GetEmpID();
   }


   public void fillgrid()
   {
       string connString = "Data Source=xxxx\\sqlexpress;Initial Catalog=ISSUsers;User=sa;Password=123456;Integrated Security=false";
       string sqlQuery = "SELECT Employee_ID,Employee_Name,Employee_Country,Age,Contact_Number,Employee_Mail,DateOfJoining,Department_ID,Manager,Salary FROM Employees  ORDER BY Employee_Country";   //where Employee_ID=100
       getResultSet(connString, sqlQuery);
       gridISSControl.DataSource = dsResultTable;
       gridISSControl.DataBind();
    }

   public DataSet getResultSet(string connectionString, string sqlSentence) //Get dataset for Grid
   {
       try
       {
           string connString = connectionString;
           string sqlQuery = sqlSentence;
           dsResultTable = new DataSet();
           dt = new DataTable();
           SqlCommand SqlCmdObject = new SqlCommand(sqlQuery, SqlConObject);
           SqlDataAdapter da = new SqlDataAdapter(SqlCmdObject);
           da.Fill(dsResultTable);
       }
       catch (Exception e)
       {
           lblError.Text = e.Message;
       }
       finally
       {
           SqlConObject.Close();
       }
       return dsResultTable;
   }

  public void CreateDatatableofSelectedRows() //Get Employee ID's
   {
       ArrayList names = new ArrayList();
       foreach (GridViewRow gvr in this.gridISSControl.Rows)
       {
           if (((System.Web.UI.WebControls.CheckBox)gvr.FindControl("chkSelect")).Checked == true)
           {
               names.Add(gvr.Cells[1].Text);
           }
       }

       this.lblError.Text = string.Empty;
       foreach (object itm in names)
       {
           //Create another  datatable here if possible..!!!!
       }
     }



I need to Select the records which the user has selected via the textbox into a datatable or a dataset.Pls help it...
Posted
Updated 5-Oct-12 0:52am
v4
Comments
AshishChaudha 5-Oct-12 6:46am    
What problem do you have???
ajithk444 5-Oct-12 6:50am    
i need to select the records which are selected by the user using the checkboxin the grid into datatable.
ajithk444 5-Oct-12 6:52am    
I have updated the question.pls check it...

1 solution

please review this and also read comment it might help you.

C#
public void CreateDatatableofSelectedRows() //Get Employee ID's
        {
            //ArrayList names = new ArrayList();
            //
            //Decalre DataTable object
            DataTable _selectedRows = new DataTable();
            //Add column as here you only store one value so i have decalre only one columns
            //but you can decalre morethen as per your requirement. simply add it to DataTable
            _selectedRows.Columns.Add("Name");
            DataRow _dr;
            foreach (GridViewRow gvr in this.gridISSControl.Rows)
            {
                if (((System.Web.UI.WebControls.CheckBox)gvr.FindControl("chkSelect")).Checked == true)
                {
                    //Create new row for your DataTable
                    _dr = _selectedRows.NewRow();
                    //Add value to new rows column
                    _dr["Name"] = gvr.Cells[1].Text;
                    //Add newly row to DataTable
                    _selectedRows.Rows.Add(_dr);
                    //names.Add(gvr.Cells[1].Text);
                }
            }
            //Your database of selected rows is created use it.
            _selectedRows.AcceptChanges();
            //
            //now you can use this __selectedRows DataTable directly where you need to use
            //if you need to use this outside this method then please return that table as result of this method
            //and change return type from void to DataTable
            //
            //avoid to use another loop if you can make it done with only one loop
            //
            //this.lblError.Text = string.Empty;
            //foreach (object itm in names)
            //{
            //    //Create another  datatable here if possible..!!!!
            //}
        }
 
Share this answer
 
Comments
ajithk444 8-Oct-12 6:12am    
thanks Tejas:)

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