Hello Sir,
I'm trying to display the data from database based on dropdown selection in xamarin forms cross platform
below is the code I'm using, but here nothing is getting displayed. please help me
Thanks
page3.xaml.cs
Rostercs roles = new Rostercs();
roles.staff_name = role.SelectedItem.ToString();
string url = "http://********/api/StaffRoster/details";
HttpClient client = new HttpClient();
string JsonData = JsonConvert.SerializeObject(roles);
StringContent content = new StringContent(JsonData, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url,content);
string result = await response.Content.ReadAsStringAsync();
var details = JsonConvert.DeserializeObject<List>(result);
Models1.ItemsSource = details;
////web api
public IEnumerable details( Rostercs name )
{
List staffroster = new List();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from test where rowstate=1 and staff_name='"+name.staff_name+"' order by date asc";
cmd.Connection = con;
con.Open();
SqlDataReader staff_roster = cmd.ExecuteReader();
while (staff_roster.Read())
{
Rostercs roster = new Rostercs();
roster.staff_name = staff_roster["staff_name"].ToString();
roster.day = staff_roster["day"].ToString(); staffroster.Add(roster); } con.Close(); return staffroster; }
var response = await client.PostAsync(url,content); string result = await response.Content.ReadAsStringAsync(); var details = JsonConvert.DeserializeObject<List>(result); Models1.ItemsSource = details;
From above code, you binded details
to the ItemsSource of Models1
.
And the details
is coming from string result = await response.Content.ReadAsStringAsync();
.
So , you should make sure you could get data from await response.Content.ReadAsStringAsync();
.
After that ,you can refer to my solution:
public ObservableCollection<MyViewModel> myCollection { get; set; } string result = await response.Content.ReadAsStringAsync(); List<VeggieViewModel> details = JsonConvert.DeserializeObject<List<MyViewModel>>(result); myCollection = new ObservableCollection<MyViewModel>(details); lstView.ItemsSource = myCollection;
Answers
From above code you posted, we found that you directly assign the data list to the
ItemsSource
of your listview(Models1
).Gerenally, we usually define
ObservableCollection<T>
for our listview's ItemsSource .Once the binded data is updated, then the UI will refresh automatically.
For example, you can convert the
details
toObservableCollection<MyViewModel>
and assign it toModels1.ItemsSource
.We can define a ObservableCollection to store the binded datalist and suppose our model is
MyViewModel
.Then we can do like this:
Don't you need a type here?
so:
What are the property names you're binding to on Models1?
@ledragon ,
public class staff_roster
{
public string staff_name { get; set; }
I'm trying to filter the listview data based on picker selection and display the same in listview.Please help.Thanks
Create a type with the same property name in your Xamarin app, then as jexh mentions cast to an ObservableCollection.
To filter Listview create a property (e.g. selectedStaffName) on your viewmodel and bind
...on the Picker to this property.
Then you can use Behaviours to bind "Unfocused" event to a command on the viewmodel to filter the Listview ItemSource.
@ledragon , please can you share a sample example for filtering in listview .Please help me.Thanks
Took me 3 seconds to find this:
https://montemagno.com/xamarin-forms-filtering-a-listview-with-a-bindable-picker/
@ledragon , I've gone through the given link, but in my example i am using web api. unable to bind it using picker selection.Please help me thanks
You've been given everything you need in this post.
Have you implemented jexh solution:
If so, does the details property contain your API data?
@ledragon , the web api is working correctly,please help me.thanks
That wasn't the question. Does the "details" property in "page3.xaml.cs" contain your API data?
If not then you haven't implemented the answer as provided so far.
From above code, you binded
details
to the ItemsSource ofModels1
.And the
details
is coming fromstring result = await response.Content.ReadAsStringAsync();
.So , you should make sure you could get data from
await response.Content.ReadAsStringAsync();
.After that ,you can refer to my solution: