从gridview选中拖拽的多行数据并放在一个datatable中执行DataTable中的查询返回新的DataTable

时间:2021-11-23 20:12:43

从gridview选中拖拽的多行数据并放在一个datatable中

            int[]rows = dataGridView.GetSelectedRows();

            List<DataRow> row = newList<DataRow>();

            for(int i = 0; i < rows.Length; i++)

            {            

                row.Add(dataGridView.GetDataRow((int)rows[i]));

            } if (row.Count != 0)

            {

                DataTable dt1 = row[0].Table;

                DataTable dt = newDataTable();

                dt = dt1.Clone();

                foreach (DataRowdr in row)

                {

                   dt.ImportRow(dr);

                }

2.把一个datatable放到多个datatable中、

dt = workItem.State["dtDxlivery"]as DataTable;

            DataTabledt1 = GetNewDataTable(dt, "支付方式='4'");

            DataTabledt2 = GetNewDataTable(dt, "支付方式<>'4'");

            DataTabledt3 = GetNewDataTable(ds.Tables[0], "template_type='普通快递'");

            DataTabledt4 = GetNewDataTable(ds.Tables[0], "template_type='到付快递'");

            DataSetdss = new DataSet();

            foreach(DataRow r indt4.Rows)

            {

                string str = r[4].ToString() + "到付快递";

                string printer = r[3].ToString();

                string template = r[1].ToString();

                DataTable dtt = newDataTable(str);

                dtt =GetNewDataTable(dt1, "物流公司='" + r[4].ToString() + "'");

                if (dtt.Rows.Count != 0)

                {

                    if ((printer == "")|| (template == ""))

                    {

                        MessageBox.Show("请为物流匹配好打印机和打印机模板");

                        this.Dispose();

 

                    }

                    else

                    {

                        print(dtt,str, printer, template);

                    }

                }

/// 执行DataTable中的查询返回新的DataTable

        /// </summary>

        /// <paramname="dt">源数据DataTable</param>

        /// <paramname="condition">查询条件</param>

        /// <returns></returns>

        privateDataTable GetNewDataTable(DataTable dt, stringcondition)

        {

            DataTablenewdt = new DataTable();

            newdt = dt.Clone();

            DataRow[]dr = dt.Select(condition);

            for(int i = 0; i < dr.Length; i++)

            {

                newdt.ImportRow((DataRow)dr[i]);

            }

            returnnewdt;//返回的查询结果

        }