I know this has been asked before, but I am missing a step in databinding a LINQ query into a dropdown list.
The error was that it was not finding the subject.
var querydd = from b in com.Communications
select b.subject;
notesdd = new DropDownList();
notesdd.ID = "notesdd" + i.ToString(); //Subject line
notesdd.DataSource = querydd;
notesdd.DataTextField = "subject";
notesdd.DataBind();
Your linq query creates just a collection of strings, not objects with property "subject". So what you should do is just bind this list to the drop down directly:
notesdd = new DropDownList();
notesdd.DataSource = querydd.ToList();
notesdd.DataBind();
The data that you bind to notesdd does indeed - as the error tells you - not contain a property with the name subject. Why?
While from b in com.Communications select b would return a collection of objects that contain a subject-property, you are explicitly selecting only that property from your objects! This results in a list of <whatever type subject has>.
So either change your query to the one I stated above or change your DataValueField, i.e. don't specify it at all!
Related
I have for the most part successfully connected to an API endpoint and manage to deserialize nested json result and bind it to a gridview using Newtonsoft.Json serialization attributes.
I however cannot bind to a dropdownlist. What is the correct property name to use to pass the supplier name to my dropdownlist?
I can see the property I want to pass (supplier name) and have tried all possible strings I can think of but all I get is the class name to display.
The Supplier Name displays fine on the gridview
I can see the property I want to display supplier -> name
Binding Code
var readdata = comsumeapi.Result;
if (readdata.IsSuccessStatusCode)
{
var displayrecords = readdata.Content.ReadAsAsync<IList<CoupaPODetails>>();
displayrecords.Wait();
empobj = displayrecords.Result;
GridView1.DataSource = empobj;
GridView1.DataBind();
DropDownList1.DataSource = empobj;
DropDownList1.DataTextField = "supplier";
DropDownList1.DataBind();
}
It would have been quite helpful to see your JSON object code but I think I can glean what I need from the screenshots
You've bound the drop down list to supplier object, not the name of the supplier. I think you should probably make a new list of all the different suppliers and bind to that, something like:
var x = empobj.Select(e => e.supplier.name).Distinct().ToList();
(Your supplier object only seems to contain a name? This a bit odd why there would even be a supplier object at all if it only houses a string. I figured it might contain more than that , like a name and an ID. If it contains more than that and you want a display text and a value that are different, use one of the techniques from here to group by eg the value and then linq .Select(g => new ListItem(){Text = g.First(), Value = g.Key}) to generate a List<ListItem> that can be the datasource for your drop down)
Don't forget that you'll also need to bind to the grid's row data bound event to set the selected item in the drop down, detail for which is here
I have a checkedListBox that has been populated from a Linq To SQL query (I do not want to bound the list directly to the Linq query).
I populate it with:
var selected = from c in dc.Personnel
select new { Item = c.PersonnelID, Description = c.FirstName + " " + c.Surname };
foreach (var item in selected)
myList.Items.Add(item.Item, item.Description);
myList.DisplayMember = "Description";
myList.ValueMember = "Item";
Now I want to retrieve the ValueMember at position x like:
myList.GetItem(0).ValueMember
Obviously that is not correct, but I'm not sure what the correct method is. Statement above have compile error that 'object does not contain definition for ValueMember'.
All you should need to do is use myList.GetItem(0). Since you've set your myList.ValueMember ahead of time, it will return the value stored in the Item property of the bound item, i.e. item.Item.
ValueMember is not the value itself, it's the name of the "member", i.e. the property, on the bound objects that will represent the values contained in the list.
Here is the doc: https://msdn.microsoft.com/en-us/library/3yx132k0%28v=vs.110%29.aspx
I can't believe how difficult this simple task is.
I have the following code:
cboCountry.ValueMember = "ID";
cboCountry.DisplayMember = "Title";
var countries = from c in context.Set<sc_Countries>()
orderby c.Title
select new { c.ID, c.Title };
Now, I want to populate the ComboBox cboCountry with this collection, and then I want to select the list item with the ID (value) "US".
I was able to add the items to the ComboBox using cboCountry.Items.AddRange(countries.ToList()), but then cboCountry.SelectedValue = "US" had no effect.
Next, I tried adding the collection using cboCountry.DataSource = countries but this just left the control list empty.
Surely there must be a simple way to accomplish this trivial task. Can anyone offer the missing ingredient?
Until you call ToList() on your LINQ statement, you're not actually getting data from the database:
var countries = (from c in context.Set<sc_Countries>()
orderby c.Title
select new { c.ID, c.Title }).ToList();
Now you should be able to set the DataSource, etc. like you were:
cboCountry.ValueMember = "ID";
cboCountry.DisplayMember = "Title";
cboCountry.DataSource = countries;
cboCountry.SelectedValue = "US"
Edit:
Now that I'm re-reading your question, it looks like you were already calling countries.ToList(), but using Items.AddRange. I see the same thing you do when I try it. It appears you have to set the DataSource instead of using Items.AddRange, for SelectedValue to work.
Situation:
I am attempting to bind a BindingList<string[]> constructed from a LINQ to SQL query to a DataGridView.
Problem:
I either cannot make modification to the DataGridView after items are generated -or- I get a bunch of unwanted fields in my DataGridView (it depends on which iteration of my code I use) I have googled as hard as I can and tried implementing most of the solutions I have found online to no avail.
I know that string has no public property for its actual value. I am having a difficult time determining how to retrieve that (I believe is part of the problem).
C#
int item = (from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].Modules
where p.ModuleName.Equals(clbModules.SelectedItem)
select p.ModuleId)
.FirstOrDefault();
BindingList<string[]> Data = new BindingList<string[]>((
from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].QuestionAnswers
where p[2].Equals(item)
select new string[] { p[0].ToString(), p[3].ToString() })
.ToList());
dgvQuestions.DataSource = Data;
dgvQuestions.Refresh();
Unwanted Behavior:
This occurs after binding
Question:
Why is this happening?
How do I fix it?
Additional Information:
I am not sure what additional information may be need but I will supply what is requested.
Also if I switch to my other code iteration:
int item = (from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].Modules where p.ModuleName.Equals(clbModules.SelectedItem) select p.ModuleId).FirstOrDefault();
var Data = new BindingList<object>((from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].QuestionAnswers where p[2].Equals(item) select new {Question = p[0].ToString(), Answer = p[3].ToString() }).Cast<object>().ToList());
dgvQuestions.DataSource = Data;
dgvQuestions.Refresh();
dgvQuestions.Columns[1].ReadOnly = false;
I can see the data properly but I cannot edit the column I would like to.
You are binding to a list of string arrays, and you are getting the properties form the array. Most likely you want something like the following:
var Data = new BindingList<object>((
from p in CurrentConversion.Companies[lbCompanies.SelectedIndex].QuestionAnswers
where p[2].Equals(item)
select new {
Val1 = p[0].ToString(),
Val2 = p[3].ToString()
}).ToList());
The reason you're seeing those fields in the Grid is that you're binding each row to a string[]. So it is automatically displaying the properties of string[] as the columns. There is no built-in logic for the grid to parse an array and use the contents of the array as columns.
In order to get the DataGrid to display your data correctly, you should bind it to a custom type, and it will use the public properties of the type as columns.
I tried to load top3 record of table into DataGrid it shows error Cannot implicitly convert type 'System.Linq.IQueryable' to 'string'.The code below what i wrote
MyDatatBaseDataContext Mydb = new MyDatatBaseDataContext();
var top3 = (from t in Mydb.GetTable<student>() select t).Take(2);
grd_8.ItemStringFormat = top3;
Change var top3 = (from t in Mydb.GetTable<student>() select t).Take(2);
to
List<student> top3 = (from t in Mydb.GetTable<student>() select t).Take(2).ToList();
and if grd_8.ItemStringFormat is a string then you have to convert yr List to a string using StringBuilder
but You can assign the List<student> top3 to Your grid ItemsSource/DataSource
ItemStringFormat Gets or sets a composite string that specifies
how to format the items in the ItemsControl if they are displayed as
strings.
Why are you assigning the LINQ results to ItemStringFormat. I believe you are looking for the ItemsSource property
grd_8.ItemsSource = top3;
Also, to select first three records from the query use Take(3) not Take(2)
Why are you assigning your results to the ItemStringFormat?
grd_8.ItemStringFormat = top3;
You need to assign the results to the ItemSource
grd_8.ItemSource = top3;