I m trying to Create DataTable At runtime with this code..
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..
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
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