Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET .NET C#4.0 Gridview , +
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...
 
 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 5-Oct-12 1:42am
Edited 5-Oct-12 1:52am
v4
Comments
AshishChaudha at 5-Oct-12 6:46am
   
What problem do you have???
ajithk444 at 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 at 5-Oct-12 6:52am
   
I have updated the question.pls check it...

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

please review this and also read comment it might help you.
 
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..!!!!
            //}
        }
  Permalink  
Comments
ajithk444 at 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)

  Print Answers RSS
0 OriginalGriff 431
1 Afzaal Ahmad Zeeshan 184
2 /\jmot 176
3 Sergey Alexandrovich Kryukov 175
4 Marcin Kozub 175
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100