Hi Richa,
Check this example. Now please take its reference and correct your code.
For sum i have used DataTable Compute function. You can refer below article for more details.
HTML
<asp:DropDownList runat="server" ID="ddlYears" AutoPostBack="true" OnSelectedIndexChanged="YearChanged">
<asp:ListItem Text="2018" Value="2018" />
<asp:ListItem Text="2019" Value="2019" />
<asp:ListItem Text="2020" Value="2020" />
<asp:ListItem Text="2021" Value="2021" />
<asp:ListItem Text="2022" Value="2022" />
</asp:DropDownList>
<br />
Sum of values for all the years for Region = PM à <b>
<asp:Label ID="lblPM" runat="server" /></b>
<br />
Sum of values for all the years for Region = MSw à <b>
<asp:Label ID="lblMSw" runat="server" /></b>
<br />
Sum of values for all the years for Region = MS à <b>
<asp:Label ID="lblMS" runat="server" /></b>
<br />
<asp:GridView runat="server" ID="gvDetails" />
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = GetData();
gvDetails.DataSource = dt;
gvDetails.DataBind();
lblMSw.Text = dt.Compute("SUM(value)", "region = 'MSw'").ToString();
lblPM.Text = dt.Compute("SUM(value)", "region = 'PM'").ToString();
lblMS.Text = dt.Compute("SUM(value)", "region = 'MS'").ToString();
}
}
protected void YearChanged(object sender, EventArgs e)
{
DataTable dt = GetData();
DataRow[] dr = dt.Select("year>=" + ddlYears.SelectedValue);
DataTable dtFilter = dr.Length > 0 ? dr.CopyToDataTable() : dt.Clone();
gvDetails.DataSource = dtFilter;
gvDetails.DataBind();
lblMSw.Text = dt.Compute("SUM(value)", "year>= " + ddlYears.SelectedValue + " AND region = 'MSw'").ToString();
lblPM.Text = dt.Compute("SUM(value)", "year>= " + ddlYears.SelectedValue + " AND region = 'PM'").ToString();
lblMS.Text = dt.Compute("SUM(value)", "year>= " + ddlYears.SelectedValue + " AND region = 'MS'").ToString();
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("year", typeof(int)),
new DataColumn("value", typeof(decimal)),
new DataColumn("region",typeof(string)) });
dt.Rows.Add(2018, 999, "MSw");
dt.Rows.Add(2018, 11770.9, "PM");
dt.Rows.Add(2018, 37.6, "MS");
dt.Rows.Add(2019, 550.6, "PM");
dt.Rows.Add(2019, 1888.3, "MSw");
dt.Rows.Add(2019, 827.3, "MS");
dt.Rows.Add(2020, 129144.9, "MSw");
dt.Rows.Add(2020, 2466.3, "PM");
dt.Rows.Add(2020, 1692.6, "MS");
dt.Rows.Add(2021, 515.4, "PM");
dt.Rows.Add(2021, 2.5, "MS");
dt.Rows.Add(2021, 1550.3, "MSw");
dt.Rows.Add(2022, 343, "MS");
dt.Rows.Add(2022, 12, "MSw");
dt.Rows.Add(2022, 45.6, "PM");
return dt;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As DataTable = GetData()
gvDetails.DataSource = dt
gvDetails.DataBind()
lblMSw.Text = dt.Compute("SUM(value)", "region = 'MSw'").ToString()
lblPM.Text = dt.Compute("SUM(value)", "region = 'PM'").ToString()
lblMS.Text = dt.Compute("SUM(value)", "region = 'MS'").ToString()
End If
End Sub
Protected Sub YearChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData()
Dim dr As DataRow() = dt.[Select]("year>=" & ddlYears.SelectedValue)
Dim dtFilter As DataTable = If(dr.Length > 0, dr.CopyToDataTable(), dt.Clone())
gvDetails.DataSource = dtFilter
gvDetails.DataBind()
lblMSw.Text = dt.Compute("SUM(value)", "year>= " & ddlYears.SelectedValue & " AND region = 'MSw'").ToString()
lblPM.Text = dt.Compute("SUM(value)", "year>= " & ddlYears.SelectedValue & " AND region = 'PM'").ToString()
lblMS.Text = dt.Compute("SUM(value)", "year>= " & ddlYears.SelectedValue & " AND region = 'MS'").ToString()
End Sub
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("year", GetType(Integer)),
New DataColumn("value", GetType(Decimal)),
New DataColumn("region", GetType(String))})
dt.Rows.Add(2018, 999, "MSw")
dt.Rows.Add(2018, 11770.9, "PM")
dt.Rows.Add(2018, 37.6, "MS")
dt.Rows.Add(2019, 550.6, "PM")
dt.Rows.Add(2019, 1888.3, "MSw")
dt.Rows.Add(2019, 827.3, "MS")
dt.Rows.Add(2020, 129144.9, "MSw")
dt.Rows.Add(2020, 2466.3, "PM")
dt.Rows.Add(2020, 1692.6, "MS")
dt.Rows.Add(2021, 515.4, "PM")
dt.Rows.Add(2021, 2.5, "MS")
dt.Rows.Add(2021, 1550.3, "MSw")
dt.Rows.Add(2022, 343, "MS")
dt.Rows.Add(2022, 12, "MSw")
dt.Rows.Add(2022, 45.6, "PM")
Return dt
End Function
Screenshot