2011年11月15日 星期二

C# DataGridView中存檔按鈕的程式碼

    private void save_bt_Click(object sender, EventArgs e)
        {
           // MyDataTable.AcceptChanges();
            int p1 = MyDataTable.Rows.Count;
            System.Collections.ArrayList SQLStringList = new System.Collections.ArrayList();
            DataRowState rowState = new DataRowState();
           
            for (int i = 0; i < p1; i++)
            {
                DataRow dataRow = MyDataTable.Rows[i];
                rowState = MyDataTable.Rows[i].RowState;               
                if (rowState == DataRowState.Modified)
                {
                    string P_Str_cmdtxt = "UPDATE ST_PURC1 SET KINDNO =";
                   // MyDataTable.Rows[i].ce.c..CC.Columns
                    //P_Str_cmdtxt += "'" + this.dataGridView1.Rows[i].Cells["KINDNO"].Value + "'";
                    P_Str_cmdtxt += "'" + MyDataTable.Rows[i]["KINDNO"]+ "'";
                    P_Str_cmdtxt += " WHERE PURNO='" + this.textPURNO.Text + "'";
                    P_Str_cmdtxt += " AND   CODE='" + MyDataTable.Rows[i]["CODE"] + "'";
                    SQLStringList.Add(P_Str_cmdtxt);
                }
                else if (rowState == DataRowState.Added)               
                {
                    string P_Str_cmdtxt = "INSERT INTO ST_PURC1 ";
                    P_Str_cmdtxt += "(PURNO ";
                    P_Str_cmdtxt += ",CODE ";
                    P_Str_cmdtxt += ",KINDNO ";
                    P_Str_cmdtxt += ")";                   
                    P_Str_cmdtxt += "VALUES ";
                    P_Str_cmdtxt += "('" + this.textPURNO.Text + "'";
                    P_Str_cmdtxt += ",'" + MyDataTable.Rows[i]["CODE"] + "'";
                    P_Str_cmdtxt += ",'" + MyDataTable.Rows[i]["KINDNO"] + "'";
                    P_Str_cmdtxt += ")";                   
                    SQLStringList.Add(P_Str_cmdtxt);
                }
                else if (rowState == DataRowState.Deleted)
                {
                     string P_Str_cmdtxt = "DELETE ST_PURC1 ";
                     P_Str_cmdtxt += " WHERE PURNO='" + this.textPURNO.Text + "'";
                     P_Str_cmdtxt += " AND   CODE='" + (string)dataRow["CODE",DataRowVersion.Original] + "'";
                     SQLStringList.Add(P_Str_cmdtxt);
                     G_SqlClass.ExecuteSqlTran(SQLStringList);                   
                }

            }   
                      
            G_SqlClass.ExecuteSqlTran(SQLStringList);
            LoadData_dataGridView1();
            
        }

沒有留言:

張貼留言