Im setting the the datasource with the following code:
protected void Page_Load(object sender, EventArgs e)
{
var vacancies = from v in db.Vacancies
join c in db.Customers on v.CustomerID equals c.CustomerID
join cp in db.CustomerPortals on c.CustomerID equals cp.CustomerID
where cp.PortalID == Master.Portal.ID
select new
{
Title = v.Title,
Internship = (v.ContractID == 6),
Hours = v.Hours,
City = v.Customer.City.Name,
Degree = v.Degree.Title,
Contract = v.Contract.Title,
CustomerID = v.CustomerID
};
rVacancies.ItemDataBound += new RepeaterItemEventHandler(rVacancies_ItemDataBound);
rVacancies.DataSource = vacancies;
rVacancies.DataBind();
}
Now i want to know how i can access 1 of the columns (like CustomerID) from the ItemDataBound event.
void rVacancies_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// This doesnt seem to work, row would be null even though e.Item.DataItem has a value.
DataRow row = (DataRow)e.Item.DataItem;
}
I have figured out that e.Item.DataItem contains all the fields from my query and the type of e.Item.DataItem is
f__AnonymousType8<string,bool,byte,string,string,string,long>
Finally found it, was as simple as the following:
long customerID = long.Parse(DataBinder.Eval(e.Item.DataItem, "CustomerID").ToString());
This .Net 4.0 approach is also very cool indeed!
public void PersonDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
dynamic person = e.Item.DataItem as dynamic;
string name = person.Name;
int age = person.Age;
}
}
All Credit To:
http://www.kristofclaes.be/blog/2010/08/12/anonymous-types-and-the-itemdatabound-event/
Because the page now shows an Error 404, here is the page from the Wayback Machine:
Anonymous types and the ItemDataBound event (Archived version)
You're not binding a datarow to your control (you're binding an anonymous type), so you should not cast DataItem to DataRow.
Try to get your row's data as :
var dataItem = e.Item.DataItem;
// For example get your CustomerID as you defined at your anonymous type :
string customerId = dataItem.CustomerID;
Related
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
EmployeeDataDataContext con = new EmployeeDataDataContext();
List<Employee> employees = (from s in con.Employees
where s.Name.ToLower() == SearchBox.Text.ToLower()
select s).ToList();
EmployeeGrid.ItemsSource = employees;
}
private void Edit_Click(object sender, RoutedEventArgs e)
{
Employee selected = EmployeeGrid.SelectedItem as Employee;
if (selected == null)
MessageBox.Show("You must select the employee.");
else
{
EditEmployee employee = new EditEmployee(selected);
employee.ShowDialog();
}
}
could not getting anything in the list wheni press the search button .
It sounds like you want to do an partial match. You can use string.Contains for that
where s.Name.ToLower().Contains(SearchBox.Text.ToLower())
That will allow your text to match anywhere within the name column.
I want to display details of a selected item from a combo box in wpf using entity framework. But my code below only displays the first entry from database no matter what item is selected.
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (Entities c = new Entities())
{
string sFirst = c.UserProfiles.FirstOrDefault().First.ToString();
string sLast = c.UserProfiles.FirstOrDefault().Last.ToString();
txtFirst.Text = sFirst;
txtSecond.Text = sLast;
}
}
I think you are mixing up stuffs.
I believe what you actually want is the item within the combox.
Try this code:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBox = sender as ComboBox;
if (comboBox != null)
{
var item = comboBox.SelectedItem as EntityType;
//EntityType == the table you are loading into combobox (I guess it supposed to be UserProfile)
if (item != null)
{
txtFirst.Text = item.First.ToString();
txtSecond.Text = item.Last.ToString();
}
}
}
You are not filtering the data from the database... there is no where clause that specifies the selected item. I have no idea on the structure of your data but try something like this:
using (Entities c = new Entities())
{
string sFirst = c.UserProfiles.Where(u => u.Id == selectedItemId).First().First.ToString();
string sLast = c.UserProfiles.Where(u => u.Id == selectedItemId).First().Last.ToString();
txtFirst.Text = sFirst;
txtSecond.Text = sLast;
}
Furthermore, there is no point in using FirstOrDefault() if you are not going to check whether the result is null or not.
My first post here and Iam an absolute beginner. Searched the web for hours. I feel that I might have approached my problem the wrong way, but here goes.
I have a Datasource that displays Loans (assets) in a Gridview.
I would like to have a ddl to filter loans. Like: `If returneddate !=null the items in grid will be free for a new loan.
Selecting ex. "Available assets" in ddl runs a where query on entitydatasource and retrieves the filtered data into grid.
My code: A bit of a mess, several queries that hopefully do the same. I prefer the first one LinqtoEntities
namespace Logsys.Pages
{
public partial class OversiktLån : Page
{
private LogsysEntities context = new LogsysEntities();
protected void Page_Load(object sender, EventArgs e)
{
}
protected EntityDataSource LaanLedig(object sender, EventArgs e)
{
var ledigQuery = from laan in context.Laans
where laan.Returnertdato != null
select laan;
foreach (var laan in ledigQuery)
{
}
}
protected void DDLlaan_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void LaanEntityDataSource_QueryCreated(object sender, QueryCreatedEventArgs e)
{
var laanQuery1 = e.Query.OfType<Laan>();
e.Query = from c in laanQuery1
where (c.Returnertdato != null)
select c;
How to get result of query "into" datasource and make ddl items trigger queries?
KK
protected void ddlLaan_SelectedIndexChanged(object sender, EventArgs e)
{
Int32 ddlvalue = Convert.ToInt32(ddlLaan.SelectedValue);
if (ddlvalue == 1)
{
CLogsysEntities = new LogsysEntities();
var ledig =
from laan in CLogsysEntities.Laans
where laan.Returnertdato != null
select laan;
LaanGridView.DataSourceID = null;
LaanGridView.DataSource = ledig.ToList();
LaanGridView.DataBind();
Here is my code;
comboBoxAdminVisit.DataSource = be.Events;
comboBoxAdminVisit.DisplayMember = "EventName";
var fff = (from cc in be.Visitors
select cc.Attending).FirstOrDefault();
var ggg = (from xx in be.Events
where xx.Id == fff
select xx.Id).FirstOrDefault();
if (fff == ggg)
{
foreach (var name in comboBoxAdminName.Items)
{
comboBoxAdminName.Items.Add(name);
}
cc.Attending is a foreign key, it stores the priomary key of the event tabel. I want to select an event from the first combo box and have the second populated with those attending the event. Thanks in advance!
Events Visitors
------ --------
Id Id
Attendee(FK) Name
EventName Company
EventStart Car Reg
EventEnd Visiting
I don't know exact structure of your entities, but here is an idea how to implement it:
private void Form1_Load(object sender, EventArgs e)
{
comboBoxAdminVisit.DataSource = be.Events;
comboBoxAdminVisit.DisplayMember = "EventName";
}
private void comboBoxAdminVisit_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBoxAdminVisit.SelectedItem != null)
{
Event selectedEvent = (Event)comboBoxAdminVisit.SelectedItem;
var visitors = (from cc in be.Visitors
where cc.Attending.Events.Contains(x => x.EnventId = selectedEvent.Id)
select cc);
comboBoxAdminName.DataSource = visitors;
comboBoxAdminName.DisplayMember = "Name";
}
}
I already retrieved my database(DescriptionCode) on dropdownlist inside on a repeater.
Now, I'm trying to save/add/insert on my database the selected value of dropdownlist but i
failed.
Any assistance gratefully received. Thanks!
protected void GeneralRepeater_OnItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList myDDL = (DropDownList)e.Item.FindControl("GeneralDDL");
Diagnosis oDiagnosis = new Diagnosis();
PlanOfCare oPlanOfCare = new PlanOfCare();
DataView dv = new DataView(oDiagnosis.GetDiagnosis());
myDDL.DataSource = PatientDiagnosis1;
myDDL.DataTextField = "DiagnosisCode";
myDDL.DataValueField = "DiagnosisCode";
myDDL.DataBind();
//PUT AN EMPTY FIELD FOR DROPDOWNLIST
ListItem LI = new ListItem("", "");
myDDL.Items.Insert(0, LI);
myDDL.SelectedValue = "0";
}
}
protected void cmdSave_Click(object sender, EventArgs e)
{
oPlanofCareSave.DiagnosesCode = //[1]this must the selected value of dropdownlist inside of repater
PlanSave(ooPlanofCareSave);
}
What about the cmdSave button, is that is inside the Repeater
You need to find out myDDL inside each row and for each of them you need to get this value.
foreach (RepeaterItem rptItem in RepeaterName.Rows)
{
DropDownList myDDL = (DropDownList)rptItem.FindControl("myDDL");
}
DropDownList myDDL = (DropDownList)GeneralRepeater.Items[indexvalue].FindControl("GeneralDDL");
oPlanofCareSave.DiagnosesCode = myDDL.SelectedValue;
it is easy to do it like that