Mega Code Archive

 
Categories / C# / ADO Database
 

Use DataViewRowState to filter DataGridView

DataSourceDataView using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Configuration; using System.Data.SqlClient; // public class Form1 : Form {     public Form1() {         InitializeComponent();     }     private void getData_Click(object sender, EventArgs e) {         using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["northwind"].ConnectionString)) {             string select = "SELECT * FROM products";             SqlDataAdapter da = new SqlDataAdapter(select, con);             DataSet ds = new DataSet();             da.Fill(ds, "Products");             originalData.AutoGenerateColumns = true;             originalData.DataSource = ds.Tables["Products"];             DataView dv = new DataView(ds.Tables["Products"]);             filteredData.AutoGenerateColumns = true;             filteredData.DataSource = dv;             comboBox1.SelectedIndex = 6;             comboBox1.Enabled = true;         }     }     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {         DataViewRowState state;         switch (comboBox1.Text) {             case "Added":                 state = DataViewRowState.Added;                 break;             case "CurrentRows":                 state = DataViewRowState.CurrentRows;                 break;             case "Deleted":                 state = DataViewRowState.Deleted;                 break;             case "ModifiedCurrent":                 state = DataViewRowState.ModifiedCurrent;                 break;             case "ModifiedOriginal":                 state = DataViewRowState.ModifiedOriginal;                 break;             case "None":                 state = DataViewRowState.None;                 break;             case "OriginalRows":                 state = DataViewRowState.OriginalRows;                 break;             case "Unchanged":                 state = DataViewRowState.Unchanged;                 break;             default:                 state = DataViewRowState.OriginalRows;                 break;         }         try {             ((DataView)filteredData.DataSource).RowStateFilter = state;         } catch (Exception ex) {             MessageBox.Show(ex.ToString());         }     }     private void InitializeComponent() {         this.originalData = new System.Windows.Forms.DataGridView();         this.getData = new System.Windows.Forms.Button();         this.comboBox1 = new System.Windows.Forms.ComboBox();         this.filteredData = new System.Windows.Forms.DataGridView();         this.label1 = new System.Windows.Forms.Label();         ((System.ComponentModel.ISupportInitialize)(this.originalData)).BeginInit();         ((System.ComponentModel.ISupportInitialize)(this.filteredData)).BeginInit();         this.SuspendLayout();         //          // originalData         //          this.originalData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)                     | System.Windows.Forms.AnchorStyles.Left)                     | System.Windows.Forms.AnchorStyles.Right)));         this.originalData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;         this.originalData.Location = new System.Drawing.Point(12, 12);         this.originalData.Name = "originalData";         this.originalData.Size = new System.Drawing.Size(600, 214);         this.originalData.TabIndex = 0;         //          // getData         //          this.getData.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));         this.getData.Location = new System.Drawing.Point(536, 501);         this.getData.Name = "getData";         this.getData.Size = new System.Drawing.Size(75, 23);         this.getData.TabIndex = 1;         this.getData.Text = "Get Data";         this.getData.UseVisualStyleBackColor = true;         this.getData.Click += new System.EventHandler(this.getData_Click);         //          // comboBox1         //          this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;         this.comboBox1.Enabled = false;         this.comboBox1.FormattingEnabled = true;         this.comboBox1.Items.AddRange(new object[] {             "Added",             "CurrentRows",             "Deleted",             "ModifiedCurrent",             "ModifiedOriginal",             "None",             "OriginalRows",             "Unchanged"});         this.comboBox1.Location = new System.Drawing.Point(108, 232);         this.comboBox1.Name = "comboBox1";         this.comboBox1.Size = new System.Drawing.Size(502, 21);         this.comboBox1.TabIndex = 2;         this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);         //          // filteredData         //          this.filteredData.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;         this.filteredData.Location = new System.Drawing.Point(12, 259);         this.filteredData.Name = "filteredData";         this.filteredData.Size = new System.Drawing.Size(598, 236);         this.filteredData.TabIndex = 3;         //          // label1         //          this.label1.AutoSize = true;         this.label1.Location = new System.Drawing.Point(13, 233);         this.label1.Name = "label1";         this.label1.Size = new System.Drawing.Size(29, 13);         this.label1.TabIndex = 4;         this.label1.Text = "Filter";         //          // Form1         //          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;         this.ClientSize = new System.Drawing.Size(624, 536);         this.Controls.Add(this.label1);         this.Controls.Add(this.filteredData);         this.Controls.Add(this.comboBox1);         this.Controls.Add(this.getData);         this.Controls.Add(this.originalData);         this.Name = "Form1";         this.Text = "04_DataSourceDataView";         ((System.ComponentModel.ISupportInitialize)(this.originalData)).EndInit();         ((System.ComponentModel.ISupportInitialize)(this.filteredData)).EndInit();         this.ResumeLayout(false);         this.PerformLayout();     }     private System.Windows.Forms.DataGridView originalData;     private System.Windows.Forms.Button getData;     private System.Windows.Forms.ComboBox comboBox1;     private System.Windows.Forms.DataGridView filteredData;     private System.Windows.Forms.Label label1;     [STAThread]     static void Main() {         Application.EnableVisualStyles();         Application.SetCompatibleTextRenderingDefault(false);         Application.Run(new Form1());     } }