Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m trying to Create DataTable At runtime with this code..
C#
public DataTable SetDataInDT()
        {
            DataTableDyn.DT = new DataTable();
            
            DataTableDyn.DT.Columns.Add(new DataColumn("UniqueID", typeof(string)));
            DataTableDyn.DT.Columns.Add(new DataColumn("RequestDate", typeof(DateTime)));
            DataTableDyn.DT.Columns.Add(new DataColumn("Country", typeof(string)));
            DataTableDyn.DT.Columns.Add(new DataColumn("NoOfSIM", typeof(string)));
            DataTableDyn.DT.Columns.Add(new DataColumn("Priority", typeof(string)));
            DataTableDyn.DT.Columns.Add(new DataColumn("ExecutiveName", typeof(string)));
            DataTableDyn.DT.Columns.Add(new DataColumn("ExecutiveCode", typeof(string)));
            DataTableDyn.DT.PrimaryKey = new DataColumn[] { DataTableDyn.DT.Columns["UniqueID"] };
            
            return DataTableDyn.DT;
        }


Here DataTableDyn is Class Name...

Adding New Row With This Code..
C#
int count = dataGridView1.Rows.Count;
                DataTableDyn.DT = SetDataInDT();
                for (int i = 0; i < count; i++)
                {
                    cmd = new SqlCommand("InsertNewSimRequest", cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Date", dataGridView1.Rows[i].Cells["date"].Value);
                    cmd.Parameters.AddWithValue("@Country", dataGridView1.Rows[i].Cells["Country"].Value);
                    cmd.Parameters.AddWithValue("@SimNo", dataGridView1.Rows[i].Cells["noofsim"].Value);
                    cmd.Parameters.AddWithValue("@Priority", dataGridView1.Rows[i].Cells["Priority"].Value);
                    cmd.Parameters.AddWithValue("@Executivename", dataGridView1.Rows[i].Cells["Executivename"].Value);
                    cmd.Parameters.AddWithValue("@Executivecode", dataGridView1.Rows[i].Cells["Executivecode"].Value);
                    cmd.Parameters.AddWithValue("@tname", strtemp);
                    UniqueValue = dataGridView1.Rows[i].Cells["Executivecode"].Value.ToString() + dataGridView1.Rows[i].Cells["Country"].Value.ToString() + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond+DateTime.Now.Ticks;
                    if (cn.State == ConnectionState.Closed)
                    {
                        cn.Open();
                    }
                    int t = cmd.ExecuteNonQuery();

                    dr = DataTableDyn.DT.NewRow();
                    dr["UniqueID"] = UniqueValue;
                    dr["RequestDate"] = dataGridView1.Rows[i].Cells["Date"].Value;
                    dr["Country"] = dataGridView1.Rows[i].Cells["Country"].Value;
                    dr["NoOfSim"] = dataGridView1.Rows[i].Cells["Noofsim"].Value;
                    dr["Priority"] = dataGridView1.Rows[i].Cells["Priority"].Value;
                    dr["ExecutiveName"] = dataGridView1.Rows[i].Cells["Executivename"].Value;
                    dr["ExecutiveCode"] = dataGridView1.Rows[i].Cells["Executivecode"].Value;


                    DataTableDyn.DT.Rows.Add(dr);
                    DataTableDyn.DT.AcceptChanges();}

Now On Button Click I M adding DT to another DataGridView Control
C#
if (!DataTableDyn.DT.HasErrors)
            {
                
                dataGridView2.DataSource = DataTableDyn.DT;

            }

Every time i m adding rows to DT But in DataGridview new value Replaces The old value..
Here I m not sure at runtime DT having all the rows that submited to display in datagridview..



Pls Help to find out That i m missing
Posted
Updated 30-Jun-13 21:00pm
v2

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