Use DataViewRowState to filter DataGridView : DataGridView « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Database ADO.net » DataGridViewScreenshots 
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(1212);
        this.originalData.Name = "originalData";
        this.originalData.Size = new System.Drawing.Size(600214);
        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(536501);
        this.getData.Name = "getData";
        this.getData.Size = new System.Drawing.Size(7523);
        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(108232);
        this.comboBox1.Name = "comboBox1";
        this.comboBox1.Size = new System.Drawing.Size(50221);
        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(12259);
        this.filteredData.Name = "filteredData";
        this.filteredData.Size = new System.Drawing.Size(598236);
        this.filteredData.TabIndex = 3;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(13233);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(2913);
        this.label1.TabIndex = 4;
        this.label1.Text = "Filter";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(624536);
        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());
    }
}

           
       
Related examples in the same category
1.Bind DataGridView to Array
2.Data Source with Generic Collection
3.Setup Columns for DataGridView
4.Bind List to DataGridView
5.Custom DataGridView with DataGridViewTextBoxColumn, DataGridViewImageColumn,DataGridViewComboBoxColumn
6.Set up DataGridView from DataColumn
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.