Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display rows in ascending order in gridview after clicking on button. please help me, Here is the code.

protected void Button2_Click(object sender, EventArgs e)
{
try
{
DataTable dt = new DataTable();
Dbconn.Open();
string str = "select id,dte from test order by id asc";
oda = new OracleDataAdapter(str, Dbconn);
oda.Fill(dt);

GridView1.DataBind();

}

catch (Exception ex)
{

}
}

What I have tried:

protected void Button2_Click(object sender, EventArgs e)
{
try
{
DataTable dt = new DataTable();
Dbconn.Open();
string str = "select id,dte from test order by id asc";
oda = new OracleDataAdapter(str, Dbconn);
oda.Fill(dt);

GridView1.DataBind();

}

catch (Exception ex)
{

}
}
Posted
Updated 17-Apr-18 18:20pm

Please refer the following article

How to Perform Sorting in Gridview in ASP.NET[^]


In the.aspx page you need to specify the OnSorting event and need to set AllowSorting=true to the GridView. Then you need to add the sorting logic in the method specified in OnSorting event
 
Share this answer
 
v2
You need to set the GridView datasource property as per below;
C#
// Fill the datatable from your adapter
odaFill(dt);
// Set the grid data source
GridView1.DataSource = dt;
// bind the grid
GridView1.DataBind();


Kind Regards
 
Share this answer
 
Comments
Darsh12345 18-Apr-18 0:29am    
an error occured.
[InvalidOperationException: Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.]
Darsh12345 18-Apr-18 0:37am    
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<title>



<cc1:ToolkitScriptManager ID="scriptmanager1" runat="server" CombineScripts="True">


<asp:Label ID="lblmsg" runat="server">
<br \>

<asp:Label ID="Label1" runat="server" Text="currdate">
<asp:TextBox ID="txtcurrdate" runat="server" ReadOnly="true">


<asp:Label ID="Label2" runat="server" Text="changedate">
<asp:TextBox ID="txtchangedate" runat="server">
<cc1:CalendarExtender ID="CalendarExtender2" runat="server" Format="dd-MMM-yyyy"
TargetControlID="txtchangedate">


<asp:Button ID="btnsubmit" runat="server" Text="submit"
onclick="btnsubmit_Click" />



<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="HPCLMR"
CellPadding="4" ForeColor="#333333" GridLines="None" Width="228px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<columns>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="DTE" HeaderText="DATE" SortExpression="DTE" />
<asp:TemplateField AccessibleHeaderText="select">
<itemtemplate>
<asp:CheckBox ID="dateselector" runat="server" />



<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />

<!--<asp:SqlDataSource ID="HPCLMR" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnEmp %>"
ProviderName="<%$ ConnectionStrings:ConnEmp.ProviderName %>"
SelectCommand="SELECT ID, DTE FROM TEST order by id desc" >
-->

<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Ascending" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Delete" />










Darsh12345 18-Apr-18 0:37am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.IO;
using System.Net;
using System.Web.Caching;
using AjaxControlToolkit;

public partial class _Default : System.Web.UI.Page
{
OracleConnection Dbconn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnEmp"].ConnectionString.ToString());
DataTable dt;
OracleDataAdapter oda;
OracleCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtcurrdate.Text = DateTime.Now.ToString("dd-MM-yyyy");

}
}
public void SetDate()
{
try
{
{

string str = "insert into test(dte) values('" + txtchangedate.Text + "')";
cmd = new OracleCommand(str, Dbconn);
if (Dbconn.State == ConnectionState.Closed)
{
Dbconn.Open();
}
cmd.ExecuteNonQuery();

lblmsg.Text = "Date Inserted Succesfully";
lblmsg.ForeColor = System.Drawing.Color.Green;
}



}
catch (Exception ex)
{
throw ex;
}
}
public void GetDate()
{
try
{

DataTable dt = new DataTable();
string str = "select id,dte from test order by id desc";
oda = new OracleDataAdapter(str, Dbconn);
oda.Fill(dt);
txtcurrdate.Text = dt.Rows[0]["DTE"].ToString();
GridView1.DataBind();
}
catch (Exception ex)
{

}
}

protected void btnsubmit_Click(object sender, EventArgs e)
{
try
{
SetDate();
GetDate();
}
catch (Exception ex)
{

}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{

DataTable dt = new DataTable();

bool atLeastOneRowDeleted = false;

foreach (GridViewRow row in GridView1.Rows)
{

CheckBox cb = (CheckBox)row.FindControl("dateselector");
if (cb != null && cb.Checked)
{
atLeastOneRowDeleted = true;
int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
Dbconn.Open();
OracleCommand cmd = new OracleCommand("delete from test where Id=" + id, Dbconn);
int result = cmd.ExecuteNonQuery();
GridView1.DataBind();


}

}
}
catch (Exception ex)
{

}
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
DataTable dt = new DataTable();
Dbconn.Open();
string str = "select id,dte from test order by id asc";
oda = new OracleDataAdapter(str, Dbconn);
oda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

}

catch (Exception ex)
{

}
}
}
an0ther1 18-Apr-18 0:47am    
In your GridView definition you have the following;
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="HPCLMR"
CellPadding="4" ForeColor="#333333" GridLines="None" Width="228px">
Remove; DataSourceID="HPCLMR"
This may cause other issues, depending on how your datasource "HPCLMR" is populated - there isno code here that shows that..
Personally I do not use the DataSourceId.

Kind Regards
Darsh12345 18-Apr-18 0:54am    
removed.
but not dispaying grid view.
only displaying current date, change date, delete and ascending button

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900