Read + Write + Report
Home | Start a blog | About Orble | FAQ | Sites | Writers | Advertise | My Orble | Login

Sortable Gridview

May 26th 2008 10:27
C# Code for sorting gridview:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel;
using System.Configuration;

namespace WpfApplication1_grid

{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
DataSet ds = new DataSet();
public Window1()
{
InitializeComponent();
Sid.MouseDoubleClick = new MouseButtonEventHandler(Sid_MouseDoubleClick);
Sid.MouseLeftButtonUp = new MouseButtonEventHandler(Sid_MouseLeftButtonUp);
Sid.MouseDown = new MouseButtonEventHandler(Sid_MouseDown);

}

void Sid_MouseDown(object sender, MouseButtonEventArgs e)
{
//MessageBox.Show("Hi");
}

void Sid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//MessageBox.Show("Hi");

}

void Sid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//MessageBox.Show("Hi");


DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) && !(dep is ListViewItem))

{ dep = VisualTreeHelper.GetParent(dep); }
if (dep == null)
return;
DataRowView item = (DataRowView)Sid.ItemContainerGenerator.ItemFromContainer(dep);

System.Data.DataRowView value = (System.Data.DataRowView)Sid.SelectedItem;

string lstValue = "";
//System.Windows.Controls.ListBoxItem curItem = ((System.Windows.Controls.ListBoxItem)Sid.SelectedItem);
lstValue = value.Row["rollno"].ToString();


MessageBox.Show("you selected " lstValue);

//MessageBox.Show("item " item[1]);

// Do something with the item...
}

GridViewColumnHeader _lastHeaderClicked = null;
ListSortDirection _lastDirection = ListSortDirection.Ascending;

private void Sort(string sortBy, ListSortDirection direction)
{
ICollectionView dataView =
CollectionViewSource.GetDefaultView(Sid.ItemsSource);

dataView.SortDescriptions.Clear();
SortDescription sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}



void GridViewColumnHeaderClickedHandler(object sender,
RoutedEventArgs e)
{
GridViewColumnHeader headerClicked =
e.OriginalSource as GridViewColumnHeader;
ListSortDirection direction;

if (headerClicked != null)
{
if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
{
if (headerClicked != _lastHeaderClicked)
{
direction = ListSortDirection.Ascending;
}
else
{
if (_lastDirection == ListSortDirection.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
direction = ListSortDirection.Ascending;
}
}

string header = headerClicked.Column.Header as string;
Sort(header, direction);

if (direction == ListSortDirection.Ascending)
{
headerClicked.Column.HeaderTemplate =
Resources["HeaderTemplateArrowUp"] as DataTemplate;
}
else
{
headerClicked.Column.HeaderTemplate =
Resources["HeaderTemplateArrowDown"] as DataTemplate;
}

// Remove arrow from previously sorted header
if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
{
_lastHeaderClicked.Column.HeaderTemplate = null;
}


_lastHeaderClicked = headerClicked;
_lastDirection = direction;
}
}
}


private void Grid_Loaded()
{
//DataSet dsGrid = new DataSet();
//dsGrid = this.GetDataset();
SqlConnection con = new SqlConnection("Data source=cp960sw;initial catalog=vh-enh-poc;uid=sa;password=rmsindia;");
SqlCommand cmd = new SqlCommand("select * from student", con);
SqlDataAdapter sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand = cmd;
sqlDa.Fill(ds);
//return ds;
Sid.DataContext = ds.Tables[0].DefaultView;

}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Grid_Loaded();



}



}
}




XAML code :



<Window x:Class="WpfApplication1_grid.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
<ListView ItemsSource="{Binding}" Name="Sid" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Rollno" DisplayMemberBinding="{Binding Path=rollno}"/>
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding Path=firstname}"/>
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding Path=lastname}"/>
<GridViewColumn Header="DOJ" DisplayMemberBinding="{Binding Path=DOJ}"/>



</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
39
Vote


   
Subscribe to this blog 


Just this blog This blog and DailyOrble (recommended)

   

   


Comments
1 Comments. [ Add A Comment ]

Comment by Harry

May 27th 2008 00:09
What does a sorting gridview actually do?

Add A Comment

To create a fully formatted comment please click here.


CLICK HERE TO LOGIN | CLICK HERE TO REGISTER

Name or Orble Tag
Home Page (optional)
Comments
Bold Italic Underline Strikethrough Separator Left Center Right Separator Quote Insert Link Insert Email
Notify me of replies
Notify extra people about this comment
Is this a private comment?
List the Email Addresses or Orble Tags of the people you would like to be notified about this comment


One per line max of 30

List the Email Addresses or Orble Tags of the people you would like to be notified about this private comment thread. Only the people in this list will be able to see or reply to your comment.


One per line max of 30

Your Name
(for the email going out to the above list, it can be different to your Orble Tag)
Your Email Address
(optional)
(required for reply notification)
Submit
More Posts
3 Posts
2 Posts
5 Posts dating from May 2008
Email Subscription
Receive e-mail notifications of new posts on this blog:
0

Siddharth sood's Blogs

35 Vote(s)
0 Comment(s)
1 Post(s)
76 Vote(s)
0 Comment(s)
2 Post(s)
36 Vote(s)
0 Comment(s)
1 Post(s)
48 Vote(s)
0 Comment(s)
1 Post(s)
28 Vote(s)
0 Comment(s)
1 Post(s)
69 Vote(s)
0 Comment(s)
2 Post(s)
155 Vote(s)
0 Comment(s)
4 Post(s)
4123 Vote(s)
13 Comment(s)
77 Post(s)
24 Vote(s)
0 Comment(s)
1 Post(s)
23 Vote(s)
0 Comment(s)
1 Post(s)
Moderated by Siddharth sood
Copyright © 2006 2007 2008 On Topic Media PTY LTD. All Rights Reserved. Design by Vimu.com.
On Topic Media ZPages: Sydney |  Melbourne |  Brisbane |  London |  Birmingham |  Leeds     [ Advertise ] [ Contact Us ] [ Privacy Policy ]