Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Searching and Filtering Gridview in ADO.NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 72
    Comment on it

    While binding data with GridView control the two of major things that we want is to sort data and to filter data.

     

    First thing can be done by writing Sql query with order by clause.

     

    Select * from student order by StId

     

    Second thing can also be done by writing Sql query with where clause.

     

    Select * from student where StId=1

     

    But this thing can create problem if we don't know which value to access till running time and interaction with database again and again will slow down the application performance.

    DataViews is like a temporary table which gives data as per as user requirement.

     

     

    So we use it for binding data because GridView from itself cannot provide filtering or sorting .

     

    private void DemonstrateDataView()
    {
    
    // Create one DataTable with one column.
    
    DataTable myTable = new DataTable("myTable");
    
    DataColumn colItem = new DataColumn("item",Type.GetType("System.String"));
    
    myTable.Columns.Add(colItem);
    
    // Add five items.
    DataRow NewRow;
    
    for(int i = 0; i <5; i++)
    {
    NewRow = myTable.NewRow();
    NewRow["item"] = "Item " + i;
    myTable.Rows.Add(NewRow);
    }
    
    // Change the values in the table.
    myTable.Rows[0]["item"]="cat";
    myTable.Rows[1]["item"] = "dog";
    myTable.AcceptChanges();
    
    // Create two DataView objects with the same table.
    DataView firstView = new DataView(myTable);
    DataView secondView = new DataView(myTable);
    
    // Print current table values.
    PrintTableOrView(myTable,"Current Values in Table");
    
    // Set first DataView to show only modified versions of original rows.
    firstView.RowStateFilter=DataViewRowState.ModifiedOriginal ;
    
    // Print values.
    PrintTableOrView(firstView,"First DataView: ModifiedOriginal");
    
    // Add one New row to the second view.
    DataRowView myDataRowView;
    myDataRowView=secondView.AddNew();
    myDataRowView["item"] = "fish";
    
    // Set second DataView to show modified versions of current rows, or New rows.
    secondView.RowStateFilter=DataViewRowState.ModifiedCurrent |
    DataViewRowState.Added;
    
    // Print modified and Added rows.
    PrintTableOrView(secondView, "Second DataView: ModifiedCurrent | Added");
    
    }
    
    private void PrintTableOrView(DataTable t, string label)
    {
    // This function prints values in the table or DataView.
    Console.WriteLine("\n" + label);
    
    for(int i = 0; i<t.Rows.Count;i++)
    {
    Console.WriteLine("\t" + t.Rows[i]["item"]);
    }
    
    Console.WriteLine();
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: