数据视图DataView的使用

        //

        ////定义一个全局的DataView对象

        //

        DataView myDataView;

 

 DbProviderFactory factory = SqlClientFactory.Instance;

 SqlCommand cmd = new SqlCommand();

 cmd.CommandText = "select * from tbl_Copy where id < 845085";

 cmd.Connection =(SqlConnection)factory.CreateConnection();

 cmd.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;

 cmd.Connection.Open(); 

            //

            //启动一个线程来显示等待窗体 Loading Form

            //

            ThreadStart ts = new ThreadStart(Loading);

            Thread loadingThread = new Thread(ts);

            loadingThread.Start();

           

            DbDataReader dr = cmd.ExecuteReader();

            //

            //DataTable DbDataReader Load入获取数据

            //

            DataTable dt = new DataTable();

            dt.Load(dr);

            //

            //实例化 全局变量 myDataView

            //

            myDataView = new DataView(dt);

            dataGridView1.DataSource = myDataView;

            //

            //将列添加到 下拉列表框 排序使用 ?Sort

            //

            foreach (DataColumn col in dt.Columns)

            {

                comboBox1.Items.Add(col.ColumnName);

            }

            comboBox1.SelectedIndex = 0;

            //

            //DataViewRowState的所有枚举类型添加到下拉列表框中 供筛选用 for filter data 

            //          

          

 //

            //数据加载完毕 ShutDown Thread

            //

            loadingThread.Abort();

            loadingThread.Join();

            loadingThread = null;

            //

            //关闭数据库连接

            //

            cmd.Connection.Close(); 

       

       

        //

        // Loading Form 窗体

        //

        public void Loading()

        {

            frmloading newObj = new frmloading();

            newObj.ShowDialog();

        }

 

            //

//设置排序字段

//

            myDataView.Sort = comboBox1.SelectedItem.ToString();   

            //

            //   1.查询时必须设置排序字段。

            //   2.查询时从当前DataView排序字段中查找匹配的值。

            //   3.返回当前行号,未查到时 结果为 -1,  

            //

            //

            myDataView.Sort = comboBox1.SelectedItem.ToString();

            int RowID = myDataView.Find(textBox1.Text);

            MessageBox.Show("数据所在行:" + RowID);

 

 //

            //选择数据视图的版本

            //

          switch(comboBox2.SelectedIndex)

            {

                case 0:

//新添加的行

                    myDataView.RowStateFilter = DataViewRowState.Added;                

                    break;

                case 1:

                    //现在DataView里拥有的行,包括新添加的行,修改过的行,未修改的行

                    myDataView.RowStateFilter = DataViewRowState.CurrentRows;

                    break;

                case 2:

                   //已删除的行

                    myDataView.RowStateFilter = DataViewRowState.Deleted;

                    break;

                case 3:

                   //已经修改的行

                    myDataView.RowStateFilter = DataViewRowState.ModifiedCurrent;

                    break;

                case 4:

//修改行的原始值

                    myDataView.RowStateFilter = DataViewRowState.ModifiedOriginal;

                    break;

                case 5:

//无操作,无状态

                    myDataView.RowStateFilter = DataViewRowState.None;

                    break;

                case 6:

                    //原始的DataView

                    myDataView.RowStateFilter = DataViewRowState.OriginalRows;

                    break;

                case 7:

                    //从未改变的行

                    myDataView.RowStateFilter = DataViewRowState.Unchanged;

                    break;

                default :

                    break;

              

 

//

            //添加行

            //

            DataRowView dv= myDataView.AddNew();

            dv["id"] = 1;

            dv["Val1"] = "新添加";

            dv["Val2"] = "新添加";     

//

            //修改数据

            //

            myDataView[myDataView.Count - 1]["Val1"] = "修改值";

            //

            //删除数据

            //

            myDataView.Delete(myDataView.Count - 1);

//

            //用表达式筛选数据

            //

            myDataView.RowFilter = "id>845082";

 

 

你可能感兴趣的:(数据视图DataView的使用)