Jump to content

Copy row from DataSet1 to DataSet2

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Abbath1349

Abbath1349

    Newbie

  • Members
  • PipPip
  • 22 posts
Tell me please how i can copy row from DataSet1 to DataSet2 and output it in DataGridView. I used example from MSDN, But i can'nt do it to work.
Table_1 is DataSet.


private void заказатьТоварToolStripMenuItem_Click(object sender, EventArgs e)

        {

            DataTable Table = MakeNamesTable();

            DataRow work = Table.NewRow();

            //work.BeginEdit();

            //  work["Наименование Товара"] = Table_1.Tables[

            work = Table_1.Tables[0].Rows[2];

         //   Table.Rows.Add(work);

            //work.EndEdit();

            dataGridView2.DataSource = Table;

;

         

        }

        private DataTable MakeNamesTable()

        {

        

            DataTable namesTable = new DataTable("Names");

            DataColumn ProductName = new DataColumn();

            ProductName.DataType = System.Type.GetType("System.String");

            ProductName.ColumnName = "Наименование Товара";

            namesTable.Columns.Add(ProductName);

            DataColumn Producer = new DataColumn();

            Producer.DataType = System.Type.GetType("System.String");

            Producer.ColumnName = "Производитель";

            namesTable.Columns.Add(Producer);

            DataColumn ProviderName = new DataColumn();

            ProviderName.DataType = System.Type.GetType("System.String");

            ProviderName.ColumnName = "Имя Поставщика";

            namesTable.Columns.Add(ProviderName);

            DataColumn Cost = new DataColumn();

            Cost.DataType = System.Type.GetType("System.String");

            Cost.ColumnName = "Цена";

            namesTable.Columns.Add(Cost);

 

            return namesTable;

        }

}



#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
do you mean something like this?


class Program {


        static Random m_rnd;


        static Program() {

            m_rnd = new Random();

        }


        static DataSet create_dataset() {

            DataSet ds = new DataSet("sample_ds");

            ds.Tables.Add(create_datatable("tableA"));

            ds.Tables.Add(create_datatable("tableB"));

            return ds;

        }


        static DataTable create_datatable(string table_name) {

            DataTable dt = new DataTable(table_name);

            dt.Columns.Add("colA", typeof(string));

            dt.Columns.Add("colB", typeof(string));

            return dt;

        }


        static void populate_dataset(DataSet ds) {

            object[] row;

            foreach (DataTable tab in ds.Tables) {

                tab.BeginLoadData();

                row = new object[tab.Columns.Count];

                for (int i = 0; i < 10; i++) {

                    

                    for (int k = 0; k < row.Length; k++)

                        row[k] = Convert.ToString(m_rnd.Next());


                    tab.Rows.Add(row);

                }

                tab.EndLoadData();

            }

            return;

        }


        static DataSet clone_dataset(DataSet ds_source) {

            DataSet ds = ds_source.Clone();

            object[] row;

            DataTable copy_table;

            foreach (DataTable tab_source in ds_source.Tables) {

                copy_table = ds.Tables[tab_source.TableName];

                row = new object[tab_source.Columns.Count];

                copy_table.BeginLoadData();

                foreach (DataRow dr in tab_source.Select()) {

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

                        row[i] = dr[i];

                    copy_table.Rows.Add(row);

                }

                copy_table.EndLoadData();

            }

            return ds;

        }


        static void print_dataset(DataSet ds) {

            StringBuilder sb;

            Console.WriteLine("{0}", ds.DataSetName);

            foreach (DataTable tab in ds.Tables) {

                Console.WriteLine("\t{0}", tab.TableName);

                sb = new StringBuilder();

                foreach (DataColumn col in tab.Columns) {

                    sb.Append(col.ColumnName);

                    sb.Append(", ");

                }

                if (sb.Length > 2) sb.Remove(sb.Length - 2, 2);

                Console.WriteLine("\t\t{0}", sb.ToString());

                

                foreach (DataRow row in tab.Select()) {

                    sb = new StringBuilder();

                    for (int i = 0; i < tab.Columns.Count; i++) {

                        sb.Append(row[i]);

                        sb.Append(", ");

                    }

                    if (sb.Length > 2) sb.Remove(sb.Length - 2, 2);

                    Console.WriteLine("\t\t{0}", sb.ToString());

                }

            }


            return;

        }


        static void Main(string[] args) {


            //Create our initial dataset

            DataSet ds = create_dataset();

            populate_dataset(ds);


            //First Clone the dataset

            DataSet ds2 = clone_dataset(ds);


            Console.WriteLine("Original Dataset:");

            print_dataset(ds);

            Console.WriteLine();

            Console.WriteLine();


            Console.WriteLine("Copied Dataset:");

            print_dataset(ds2);

            Console.WriteLine();

            Console.WriteLine();



            Console.WriteLine("Press <Enter> to terminate...");

            Console.ReadLine();

        }

    }



sorry, just wrote that intentionally verbose, take what you want or ask questions.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users