ASP.net GridView Handle Sorting - asp.net

I have the following and I'm not sure how to implement the sorting function on the gridview? The data and paging work fine. Just not sure how to handle the sorting method? I simply want to sort columns by asc and desc.
public partial class inLinksAuthGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterAsyncTask(new PageAsyncTask(LoadSomeData));
}
public async Task LoadSomeData()
{
try
{
var client = new WebClient();
client.Credentials = new NetworkCredential("test", "test");
var myInLinks = client.DownloadStringTaskAsync("http://inlink-xxxxxx.net:5000/inlinks");
await Task.WhenAll(myInLinks);
var links = JsonConvert.DeserializeObject<Rootobject>(await myInLinks);
GridView1.DataSource = links.inlinks;
GridView1.DataBind();
}
catch (Exception ex)
{
//TODO:
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// WHAT do I do here?
}
public class Rootobject
{
public Inlink[] inlinks { get; set; }
}
public class Inlink
{
public string clicks { get; set; }
public string email { get; set; }
public string ip { get; set; }
public string keyword { get; set; }
public string timestamp { get; set; }
public string title { get; set; }
public string url { get; set; }
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="900" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" AllowSorting="True"></asp:GridView>
Everything works just have no idea how to get sorting to work?
Thanks!

Set the allowsorting property of the gridview to true like allowsorting="true" since you are trying to sort the column by asc/desc. See GridView.AllowSorting Property

Related

Add content of SELECT to LIST

I've just started learning Xamarin and i would like to add content of SELECT query to List<SearchModel>. How can i do it? I've created Object model, according to Sqlite.
List<SearchModel> searchModelsList = new List<SearchModel>();
try
{
using (sQLiteConnection = new SQLiteConnection(path))
{
try
{
sQLiteConnection.Execute("SELECT * FROM CONTENT_TABLE);
}
catch (Exception ex) {
}
}
}
catch (Android.Database.Sqlite.SQLiteException ex)
{
Log.Info("exex", ex.Message);
}
class SearchModel
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string PERSON { get; set; }
public string NAME { get; set; }
}
}
I've just started learning Xamarin and i would like to add content of SELECT query to List. How can i do it? I've created Object model, according to Sqlite.
If you want to query data from sqlite database, you can take a look the following code:
<StackLayout>
<Button
x:Name="createdb"
Clicked="createdb_Clicked"
Text="create table in sqlite" />
<Button
x:Name="btnadd"
Clicked="btnadd_Clicked"
Text="add data to sqlite" />
<Button
x:Name="btnfetdata"
Clicked="btnfetdata_Clicked"
Text="get data from sqlite" />
</StackLayout>
public partial class Page2 : ContentPage
{
public SQLiteConnection conn;
public Page2()
{
InitializeComponent();
conn= GetSQLiteConnection();
}
public SQLiteConnection GetSQLiteConnection()
{
var fileName = "SearchModel.db";
var documentPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentPath, fileName);
var connection = new SQLiteConnection(path);
return connection;
}
private void btnadd_Clicked(object sender, EventArgs e)
{
//var data = conn.Table<SearchModel>();
for(int i=0;i<20;i++)
{
SearchModel model = new SearchModel();
model.PERSON = "person "+ i;
model.NAME = "cherry "+ i;
conn.Insert(model);
}
}
private List<SearchModel> searchModelsList;
private void btnfetdata_Clicked(object sender, EventArgs e)
{
searchModelsList = conn.Table<SearchModel>().ToList();
var searchlist = conn.Query<SearchModel>("SELECT * FROM SearchModel WHERE PERSON = ?", "person 1");
}
private void createdb_Clicked(object sender, EventArgs e)
{
conn.CreateTable<SearchModel>();
}
}
public class SearchModel
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string PERSON { get; set; }
public string NAME { get; set; }
}
About querying data, you can use
var searchlist = conn.Query<SearchModel>("SELECT * FROM SearchModel WHERE PERSON = ?", "person 1");
But you can also get the entire SearchModel table, then query data from List
searchModelsList = conn.Table<SearchModel>().ToList();
var list = searchModelsList.Where(p => p.PERSON == "person 1");

How pass value to Custom renderer?

I have a custon renderer entry
public class EntryPerson: Entry
{
public int TestInt { get; private set; }
public EntryPerson(int TestInt)
{
this.TestInt = TestInt;
}
}
and in xaml in have this
<local:EntryPerson >
but i want to use so:
<local:EntryPerson TextInt="0" >
how pass untill to my render?
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
Check the following code
public class EntryPerson
{
public int TestInt { get; set; }
public EntryPerson()
{
}
public EntryPerson(int test)
{
TestInt = test;
}
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
var element = Element as EntryPerson;
var test = element.TestInt;
//Element.TextChanged += Element_TextChanged;
}
}

Windows 8 RT App - Passing info between pages

I am using SQLite to store data, I have a class:
class Customers
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(30)]
public string customerNumber { get; set; }
[MaxLength(100)]
public string customerName { get; set; }
}
How I am sending data is:
private void customer_Click(object sender, ItemClickEventArgs e)
{
Customers customer = e.ClickedItem as Customers;
if(customer != null)
{
Customers c = new Customers();
c.customerName = customer.customerName;
c.customerNumber = customer.customerNumber;
c.Id = customer.Id;
this.Frame.Navigate(typeof(customerDetail), c);
}
}
I need the data to display in text boxes on the detail page.
You need to access data on DetailsPage? Here you are:
public sealed partial class DetailPage : Page
{
public DetailPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Customers customer = (Customers)e.Parameter;
//Now you can get it from customer object
}
}
This is how I solved it:
From Page:
private void customer_Click(object sender, ItemClickEventArgs e)
{
Customers customer = e.ClickedItem as Customers;
if(customer != null)
{
Customers c = new Customers();
c.customerName = customer.customerName;
c.customerNumber = customer.customerNumber;
c.Id = customer.Id;
Frame.Navigate(typeof(customerDetail), c.Id.ToString());
}
}
To Page:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string custId = e.Parameter as string;
int custIDLookup = Convert.ToInt32(custId);
lookUp(custIDLookup);
navigationHelper.OnNavigatedTo(e);
}

add a textBox in GridView and the textbox retrieves data from a collection not from a dataSource

I want to add a textBox in GridView knowing that the textbox retrieves data from a collection like ArrayList or List(OF ..) not from a dataSource (ASP.net)
Set the DataSource in code:
// in this case the object I am using is:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public bool IsInStock { get; set; }
}
private void BindGrid()
{
// in the GetProducts() method return the collection
List<Product> r = GetProducts();
this.grdSomething.DataSource = r;
this.grdSomething.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
this.BindGrid();
}
protected void grdSomething_EditCommand(object sender, DataGridCommandEventArgs e)
{
this.grdSomething.EditItemIndex = e.Item.ItemIndex;
this.BindGrid();
}
Simply add a bound field column like this:
<asp:DataGrid runat="server" ID="grdSomething" AutoGenerateColumns="False"
OnEditCommand="grdSomething_EditCommand">
<Columns>
<asp:BoundColumn DataField="Name"></asp:BoundColumn>
<asp:EditCommandColumn EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
That's it

asp.net listview bind to object

on wcf service
public class Response
{
public string ID { get; internal set; }
public string Data { get; set; }
public List<Message> Messages { get; set; }
}
public class Message
{
public DateTime DateTime { get; set; }
public string Message { get; set; }
public string ProcessName { get; set; }
public Message()
{
DateTime = DateTime.Now;
Message = "";
ProcessName = "";
}
}
asp.net
protected void Page_Load(object sender, EventArgs e)
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
EndpointAddress endpoint = new EndpointAddress(new Uri(System.Configuration.ConfigurationManager.AppSettings["wcfserviceURL"]));
Wcfservice.wcfserviceClient wcfservice = new Wcfservice.wcfserviceClient(binding, endpoint);
Wcfservice.Response response = wcfservice.ReturnResponse();
//I need to bind the response.Messages to a listview. It should display data in 3 columns DateTime, Message and ProcessName
}
I used the datasource wizard and it created the following. But I don't know what to do next for the data to be displayed
<asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1">
</asp:ListView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="Wcfservice.Response" EntityTypeName=""
TableName="Messages">
</asp:LinqDataSource>
Try adding this to your page load, where you're trying to bind the ListView:
LinqDataSource1.DataBind();

Resources