DataTable

[一篮饭特稀原创,转载请注明出处http://www.cnblogs.com/wanghafan/archive/2012/05/07/2487635.html]

删除行

View Code
1     protected void deleteDataRow(int RowID,DataTable dt)
2     {
3         for (int i = dt.Rows.Count - 1; i >= 0; i--)
4         {
5             if (Convert.ToInt32(dt.Rows[i]["RowID"]) == RowID)
6                 dt.Rows.RemoveAt(i);
7         }
8     }

 批量删除行

View Code
 1 DataTable dt = new DataTable(); 
 2 int len = dt.Rows.Count;
 3 for (int i = len; i >=0; i--)
 4 {
 5     if (((CheckBox)Rp.Items[i].FindControl("CB")).Checked)
 6     {
 7          dt.Rows[i].Delete();
 8     }
 9 }
10 dt.AcceptChanges();     //提交对表进行的修改

错误 该行已经属于另一个表

View Code
 1 从一个TABLE中取一行放到另一个TABLE里报错: 该行已经属于另一个表。
 2 DataTable dt = new DataTable();
 3 
 4 dt = ds.Tables[0].Clone(); //克隆结构传递给dt
 5 
 6 DataRow[] dr=dtOld["Product"].Select("ProjectID=1"); //通过条件得到符合条件的行
 7 for(int i=0;i<dr.Length;i++)
 8 {
 9      //将数组元素加入表...
10      dt.Rows.Add(dr[i]); //出错提示为:该行已经属于另一个表 改成 dt.Rows.Add(dr[i].ItemArray);
11 }

对DataTable进行操作后如果赋值给DataSource务必要先AcceptChange

View Code
1  比如对DataTable进行排序后,如果直接复制给DataSource,会出现DataTable的所有行状态RowState==DataRowState.Added,使得在保存后不知不觉中出现双份数据导致键值冲突!
2 
3    e.g.
4    DataView dv = dt_KeyWeightQuestionsTEO.DefaultView;
5    dv.Sort = "ParentID,OwnID";
6    dt_KeyWeightQuestionsTEO = dv.ToTable();
7    dt_KeyWeightQuestionsTEO.AcceptChanges();   <----- 关键
8    treeList.DataSource = dt_KeyWeightQuestionsTEO;

添加一自增列至首列

View Code
1 DataColumn dcIID=new DataColumn("IID", System.Type.GetType("System.Int32"));
2 dcIID.AutoIncrement = true;
3 dcIID.AutoIncrementSeed = 1;
4 dcIID.AutoIncrementStep = 1;
5 dcIID.ReadOnly = true;
6 dt.Columns.Add(dcIID);

利用 栈 实现 多行多次 撤销 Ctrl+Z 

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using DevExpress.Utils.Commands;
  9 
 10 namespace StackProgram
 11 {
 12     public partial class StackProgram : Form
 13     {
 14         private int stackLength = 3; //撤销上限        
 15         Stack<List<DataRow>> st_DataRow = new Stack<List<DataRow>>();  //
 16 
 17         public StackProgram()
 18         {
 19             InitializeComponent();
 20             DataTable dt = new DataTable();
 21             dt.Columns.Add("IID", Type.GetType("System.Int32"));
 22             dt.Columns.Add("IName", Type.GetType("System.String"));
 23             for (int i = 1; i <= 5; i++)
 24             {
 25                 DataRow dr = dt.NewRow();
 26                 dr["IID"] = i;
 27                 dr["IName"] = "Name_"+i;
 28                 dt.Rows.Add(dr);
 29             }
 30             dt.AcceptChanges();
 31             gridControl1.DataSource = dt;
 32         }
 33 
 34         private void gridControl1_KeyDown(object sender, KeyEventArgs e)
 35         {
 36             if (e.Control && e.KeyCode.ToString().Equals("Z") && st_DataRow.Count > 0)
 37             {
 38                 List<DataRow> li_dr = st_DataRow.Pop();
 39                 if (li_dr.Count==1) //添加或修改或删除
 40                 {
 41                     if (li_dr[0].RowState.Equals(DataRowState.Added) || li_dr[0].RowState.Equals(DataRowState.Modified) || li_dr[0].RowState.Equals(DataRowState.Deleted))
 42                     {
 43                         li_dr[0].RejectChanges();
 44                     }
 45                 }
 46                 else //批量删除
 47                 {
 48                     foreach (DataRow dr in li_dr)
 49                     {
 50                         dr.RejectChanges();
 51                     }
 52                 }
 53                 System.Windows.Forms.SendKeys.SendWait("{BS}"); //强制按下退格键可以取消该行的编辑状态!!!               
 54             }
 55         }
 56 
 57         private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
 58         {
 59             if (e.Button.Hint.Equals("添加"))
 60             {
 61                 gridView1.AddNewRow();
 62                 OverStack();
 63                 List<DataRow> li_dr = new List<DataRow>();
 64                 li_dr.Add(gridView1.GetDataRow(gridView1.FocusedRowHandle));
 65                 st_DataRow.Push(li_dr);
 66             }
 67             if (e.Button.Hint.Equals("删除"))
 68             {
 69                 OverStack();
 70                 List<DataRow> li_dr = new List<DataRow>();
 71                 foreach (int i in gridView1.GetSelectedRows())
 72                 {
 73                     li_dr.Add(gridView1.GetDataRow(i));
 74                 }               
 75                 st_DataRow.Push(li_dr);
 76                 gridView1.DeleteSelectedRows();
 77             }
 78         }
 79 
 80         private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
 81         {
 82             if (!gridView1.GetDataRow(gridView1.FocusedRowHandle).RowState.Equals(DataRowState.Detached)) //新增的行立即修改时不能算作编辑的行
 83             {
 84                 OverStack();
 85                 List<DataRow> li_dr = new List<DataRow>();
 86                 li_dr.Add(gridView1.GetDataRow(gridView1.FocusedRowHandle));
 87                 st_DataRow.Push(li_dr);
 88             }
 89         }
 90         
 91         private void OverStack()
 92         {
 93             if (st_DataRow.Count>0 && st_DataRow.Count >= stackLength) //大于栈空间时,删除栈底元素,更新栈顶元素
 94             {
 95                 List<List<DataRow>> li_li_row = new List<List<DataRow>>();
 96                 foreach (List<DataRow> li_dr in st_DataRow) //复制原有栈元素
 97                 {
 98                     li_li_row.Add(li_dr);
 99                 }
100                 li_li_row.Reverse(); //反转
101                 li_li_row.RemoveAt(0); //删除栈底元素
102                 st_DataRow.Clear(); //清空所有栈元素
103                 foreach (List<DataRow> li_dr in li_li_row) //重新入栈
104                 {
105                     st_DataRow.Push(li_dr);
106                 }
107             }
108         }
109     }
110 }

DataView的RowFilter注入攻击

View Code
 1  public static string EncodeString(string s) 
 2         { 
 3             StringBuilder sb = new StringBuilder(); 
 4             for (int i = 0; i < s.Length; i++) 
 5             { char c = s[i]; if (c == '*' || c == '%' || c == '[' || c == ']')             
 6                 sb.Append("[").Append(c).Append("]"); else if (c == '\'')            
 7                 sb.Append("''"); else             
 8                 sb.Append(c); } return sb.ToString(); 
 9         } 
10 
11    dv.RowFilter = String.Format(
12                            "名称='{0}' and 规格型号='{1}' and 单价='{2}' and 段ID='{3}'  and 编码='{4}'",
13                            Global.EncodeString(dr["名称"].ToString().Trim()),
14                            Global.EncodeString(dr["规格型号"].ToString().Trim()),
15                            Global.EncodeString(dr["单价"].ToString().Trim()),
16                            dr["段ID"].ToString().Trim() == string.Empty ? "0" : dr["段ID"].ToString().Trim(),
17                             Global.EncodeString(dr["编码"].ToString().Trim())
18                           );

 

 

posted @ 2012-05-07 16:44  一篮饭特稀  阅读(409)  评论(0编辑  收藏  举报