ASP NET Chart Control sorting

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

All ASP .NET Chart Control Text Articles and Slides

ASP.NET Charts Playlist

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this video we will discuss ASP.NET Chart Control sorting. This is continuation to Part 8. Please watch Part 8 before proceeding.

One DropDownList will contain the fields to sort the data by i.e
StudentName
TotalMarks

The other DropDownList will contain the sort direction i.e
Ascending
Descending

Here is the HTML used in the demo
[table style="font-family: Arial; border: 1px solid black"]
[tr]
[td]
Sort By :
[asp:DropDownList ID="ddlSortBy" AutoPostBack="true" runat="server"
OnSelectedIndexChanged="ddlSortBy_SelectedIndexChanged"]
[asp:ListItem Text="Student Name" Value="StudentName"][/asp:ListItem]
[asp:ListItem Text="Total Marks" Value="TotalMarks"][/asp:ListItem]
[/asp:DropDownList]
[/td]
[td]
Sort Direction :
[asp:DropDownList ID="ddlSortDirection" AutoPostBack="true" runat="server"
OnSelectedIndexChanged="ddlSortDirection_SelectedIndexChanged"]
[asp:ListItem Text="Ascending" Value="ASC"][/asp:ListItem]
[asp:ListItem Text="Descending" Value="DESC"][/asp:ListItem]
[/asp:DropDownList]
[/td]
[/tr]
[tr]
[td colspan="2"]
[asp:Chart ID="Chart1" runat="server"]
[Series]
[asp:Series Name="Series1"]
[/asp:Series]
[/Series]
[ChartAreas]
[asp:ChartArea Name="ChartArea1"]
[/asp:ChartArea]
[/ChartAreas]
[/asp:Chart]
[/td]
[/tr]
[/table]

and the code for the code-behind file
using System;
using System.Data;

namespace ChartsDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Use the default sort - Sort by TotalMarks in Ascending order
GetChartData(null);
}
}

private void GetChartData(string sortExpression)
{
DataSet ds = new DataSet();
// Read the data from XML file into DataSet
// Specify the column that contains values for X-AXIS
Chart1.Series["Series1"].XValueMember = "StudentName";
// Specify the column that contains values for Y-AXIS
Chart1.Series["Series1"].YValueMembers = "TotalMarks";
// Create the DataView
DataView dataView = ds.Tables[0].DefaultView;
// Specify how the data should be sorted
dataView.Sort = string.IsNullOrEmpty(sortExpression) ? "TotalMarks ASC" : sortExpression;
// Set sorted DataView as the DataSource for the Chart control
Chart1.DataSource = dataView;
// Finally call DataBind
Chart1.DataBind();
}

protected void ddlSortBy_SelectedIndexChanged(object sender, EventArgs e)
{
// Sort the data based on the selections in the DropDownLists
GetChartData(ddlSortBy.SelectedValue + " " + ddlSortDirection.SelectedValue);
}

protected void ddlSortDirection_SelectedIndexChanged(object sender, EventArgs e)
{
GetChartData(ddlSortBy.SelectedValue + " " + ddlSortDirection.SelectedValue);
}
}
}
Рекомендации по теме
join shbcf.ru