Secondary sorting in DevExpress GridView - c#

I'm using a DevExpress GridView in my application (My company still uses the old DevExpress v7.2). I have four columns, on which one of them is the "Priority" column. Status is an enumeration with three possible values: Critical, High and Low.
When the user wants to sort the GridView by this column, I want to sort by level of severity but also within the items with severity "Critical" (for example) I want the values to be sorted by date from the earliest to the latest.
If anyone can help that would be awesome.
Thanks!
John.

I'm not sure how your implementing your sorting but you could always intercept looking for a sort on your "Priority" column, then append a secondary sort on the date column.
I've done this type of thing before, mine is overkill for what your looking to do but the basic code would be something like this:
public void GridView_ExampleSorting(object sender, GridViewSortEventArgs e)
{
GridView gv = (GridView)sender;
DataTable dataTable = gv.DataSource as DataTable;
if (dataTable != null)
{
string sortdirection = GetNextSortDirection(e.SortExpression);
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + sortdirection;
if (e.SortExpression.ToString() == "priority")
dataView.Sort += " date DESC";
gv.DataSource = dataView;
gv.PageIndex = 0;
gv.DataBind();
}
}

In the newer versions of the grid control you could hold down the ctrl key and click on the 2'nd coulmn header to sort by that column as well as the 1'st one. But I'm not sure if it will also work on version 7 of the grid.
Update: its actually the shift key. please see here

Also grid have SortBy(columnName) method

Related

Correct way of filtering datagrid using datatables in C# WPF

I am creating a WPF application. There is a DataGrid displaying my items and search bar to filter the data. As you know we can't have specific rows from a Dataable to be referenced from another datatable. So the way I'm filtering right now is cloning my original database and adding the rows which matches search bar text to the cloned datatable. After that setting the datagrid's ItemsSource to the cloned datatable to show the filtered rows.
Now the problem is that when I edit my datagrid with filtered rows then obviously that cloned datatable is being modified not the original datatable. So how can I make those changes in the original datatable as well ?
I tried referencing rows from original datatable but that's no possible as one DataRow in memory can have only one container, the original datatable in this case.
EDIT
The answer was simple instead of using 2 DataTable use a DataView which is designed for this very purpose. See the modified code below.
My filter logic:
var filteredTable = dt.Clone();
foreach( DataRow row in dt.Rows)
{
if(row[FilterCategory].ToString().StartsWith(txtb_search.Text))
{
filteredTable.Rows.Add(row.ItemArray);
}
}
ItemsGrid.ItemsSource = filteredTable.DefaultView;
Here is how to do filtering the correct way using DataView. The filter string can have many forms depending on the requirement. sortColumn is filterColumn right now but can be any column to base sort on. Here is a quick tutorial to all this: http://www.csharp-examples.net/dataview-rowfilter/
string filterColumn = dt.Columns[columnIndex].ToString();
string filter = filterColumn + " LIKE '" + txtb_search.Text + "*'";
DataView dv = new DataView(dt, filter, sortColumn , DataViewRowState.CurrentRows);
ItemsGrid.ItemsSource = dv;

Populating DatagridView from a Foreach loop through a DataTable

I have used a database to populate Data-grids on many occasions.
However, in this instance, i can't seem to get the desired result.
I am trying to display only the people who have a birthday on the day.
i already have the dates, and the Birthday, but i can't seem to get teh rows to add to the datagridview.
here is how far i have got. But he "BirthdayGrdVw.DataSource" i can't seem to figure out.
i have been googling for 2 days.
I am new to this, and would really appreciate some assistance.
private void Tasks_VisibleChanged(object sender, EventArgs e)
{
Clients = Controller.DBQueries.GetPolicyList();
if(Clients != null)
{
foreach(DataRow row in Clients.Rows)
{
string ClientID = Clients.Rows[0]["ClientID"].ToString();
DateTime Birthdate = Controller.GetBirthdate(ClientID);
DateTime now = DateTime.Now;
if (Birthdate.Day == now.Day && Birthdate.Month == now.Month)
{
}
}
}
}
There appears to be things left out you your code, but without all of the information here is what I suggest you try if you are sure that your datatable has been populated.
Add a bindingsource object to your Windows Form by dragging it from the toolbox onto the form.
Set the it will be called bindingsource1. Set it's DataSource to the datatable like this:
BindingSource1.DataSource = "your data table name here";
And then set the DataGridView's DataSource to bindingsource1.
"Your DataGridView".DataSource = BindingSource1;
Or if you don't need some of the extra functionality that a BindingSource provides like "Filter" etc... You could bind the datatable directly to the DataGridView like:
DataGridView.DataSource = "your data table name here";
If Clients is a DataTable and you know what filtering you want to apply then why not use Filter on datatable as :
DataView dv = new DataView(Clients);
dv.RowFilter = "month(of your Birthday Col)="+now.Month+" and day(of your BirthDay Col)="+now.Day;
yourGridView.DataSource = dv.ToTable();
yourGridView.DataBind();
instead of all Foreach loop and all?

Filling a DataTable based on row values in a DataSet

I want to show some news posts on the front page of a website. The news have different categories but are stored in the same mysql database. All news items are loaded into a dataset but from there I just want to extract some specific news and show them in a ListView control.
At the moment I'am using the workaround solution of using a foreach loop on the dataset and formatting the result with html in the code behind. It works but I want all html formatting to be done in the aspx file, thus I'am looking for a better solution.
DataSet articles = db.loadAllArticles();
foreach (DataRow item in articles.Tables["articles"].Select("category = 1"))
{
result += "<h1 class='headline'>" + item["headline"] + "</h1><h2 class='introduction'>" + item["introduction"] + "</h2><p class='content'>" + item["content"] + "</p><p class='authorAndDate'>" + item["author"] + " " + item["datePosted"].ToString().Substring(0,10) + "</p><br/>";
}
lblDisplay.Text = result;
What I was hoping I could do was simply something like this:
DataSet articles = db.loadAllArticles();
ListView1.DataSource = articles.Tables["articles"].Select("category = 1");
ListView1.DataBind();
But the ListView control is not too happy about trying to bind DataRow to it or something.
The best workaround I can think of is to create a new Table within the "articles" DataSet which contains only articles of the chosen category, so something like this:
DataSet articles = db.loadAllArticles();
articles.Tables.Add("frontPageArticles");
articles.Tables["frontPageArticles"] = ???
And then thats where it stops. How can I fill the new datatable with rows from the other datatable where the value of a column is x?
-Eric.
You should be looking at binding you ListView to a DataView, it is filterable and sortable.
You can design your web form using control like repeater, datalist etc. and can bind these control to your datatable on codebehind.
An example can be found here

How to sort data in GridView in asp.net

How to sort datatable obtained by GridView according to some column.
I am trying to do something like this but it is not working.
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
if (sender.GetType() != typeof(GridView))
return;
DataTable dt = (DataTable)((GridView)sender).DataSource;
DataTable dtClone = dt.Clone();
dt.AsEnumerable().OrderBy(row => row.Field <string>(e.SortExpression));
((GridView)sender).Source(dtClone);
}
You can either re-bind the gridview to the sorted datatable, or you can apply the sort expression to the gridview itself rather than the underlying bound table.
This is a pretty good summary with links to examples:
http://msdn.microsoft.com/en-us/library/hwf94875.aspx
Add the attributes OnSorting="gridView_Sorting" AllowSorting="true" in Gridview
and SortExpression="ColumnName" in the column
and implement OnSorting Event.
check the links
Sorting the GridView's Data
How to sort the data in grid view?
How to sort data into GridView
Sorting Data in a GridView
How to sort GridView?
I've done something like this in the past to sort a DataTable:
DataTable dt = new DataTable(); // your data table
dt.DefaultView.Sort = "your_field" + "sort_direction";
DataView dv = new DataView(dt);
dv.Sort = ("your_field" + "sort_direction");
yourGridView.DataSource = dv;
When sorting, make sure to cast your table to a DataView. You'll save yourself a lot of time over manually implementing sorting methods.

Validating a value for a DataColumn

I'm using a DataGrid with edit functionalities in my project. It's handy, compared to having to edit its source data manually, but sadly, that means that I'll have to deal with validating user input a bit more.
And my problem is basically just that. When I set my DataGrid to EDIT mode, modify the values and then set it to UPDATE, what is the best way to check if a value that I've entered is, in fact, compatible with the corresponding column's data type?
i.e. (simple example)
// assuming
DataTable dt = new DataTable();
dt.Columns.Add("aa",typeof(System.Int32));
DataGrid dg = new DataGrid();
dg.DataSource = dt;
dg.DataBind();
dg.UpdateCommand += dg_Update;
// this is the update handler
protected void dg_Update(object src, DataGridCommandEventArgs e)
{
string newValue = (someValueIEnteredInTextBox);
// HOW DO I CHECK IF [newValue] IS COMPATIBLE WITH COLUMN "aa" ABOVE?
dt.LoadDataRow(newValue, true);
}
Thanks guys. Any leads would be so much help.
Can't you use a compare validator? it does check for the following datatypes
String
Double
Date
Currency
Decimal
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.comparevalidator.aspx
perhaps try some of these links:
http://msdn.microsoft.com/en-us/library/aa984376(VS.71).aspx
http://msdn.microsoft.com/en-us/library/0ye0dkkw.aspx
http://forums.silverlight.net/forums/p/134948/301233.aspx
http://www.daniweb.com/forums/thread101030.html#

Categories