I am developing Windows application (environment: Visual Studio 2010 and C#) I use a datagridview with records completed by datatable dt:
dataGridView1.DataSource = dt;
This datatable has 20 columns with 1 identity column - column[0] = autonumber, and column[1] called “RecordChecked” implemented as Boolean (checkbox).
I need to solve next problems:
I started from copying records from one datatable into another (see code below) – this code works ok, but then stacked how to add copied records below existing and then update them:
DataTable dtSource = ((DataTable)dataGridView1.DataSource);
DataTable dtTarget = new DataTable();
dtTarget = ((DataTable)dataGridView1.DataSource).Clone();
DataRow[] rowsToCopy;
rowsToCopy = ((DataTable)dataGridView1.DataSource).Select("DrawingNo='DM-3012'");
foreach (DataRow temp in rowsToCopy)
{
dtTarget.ImportRow(temp);
}
dt = dtTarget;
Thanks,
答案 0 :(得分:0)
我想我找到了解决这个问题的好方法。 1.创建存储过程(mySP)。
此SP创建临时表,我们在其中找到所有使用子句' WHERE DrawingNo = @ CopyFrom'选择的记录。
然后这个SP用如下语句更新临时表:
UPDATE#TempTbl1 SET RecChecked = 0,DrawingNo = @CopyTo WHERE DrawingNo = @CopyFrom。
然后,SP将临时表中的更新记录插入主表
最后,SP删除临时表并从主表中选择所有需要的记录。
2.现在我们可以在应用程序中运行此SP并将数据绑定到datagridview,如:
//Run SP
using (SqlCommand cmd = new SqlCommand("mySP", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CopyFrom", SqlDbType.VarChar).Value = sValueFrom;
cmd.Parameters.Add("@CopyTo", SqlDbType.VarChar).Value = sValueTo;
con.Open();
cmd.ExecuteNonQuery();
}
//Create SELECT statement
string strSelect = "SELECT Recid, RecChecked, DrawingNo, ... FROM Tbl1 WHERE DrawingNo = '" + sValueTo + "'"
//fill dataadapter
sda = new SqlDataAdapter(@strSelect, con);
dt = new DataTable();
sda.Fill(dt);
有效!