从数据集中选择值到其他数据集中 [英] Select Values from a dataset into anothr dataset

查看:72
本文介绍了从数据集中选择值到其他数据集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个数据集,该数据集将数据绑定到页面中的Gridview.我有复选框.当我单击页面中的按钮时,我需要将gridview中的选定行绑定到另一个数据集或另一个datatable.pls中,以帮助实现此目的.
请帮助我的代码进行必要的更改...

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...

推荐答案

请仔细阅读并阅读评论它可能对您有帮助.

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..!!!!
            //}
        }


这篇关于从数据集中选择值到其他数据集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆