hi m trying fill the combo box from cities database but i wan the metropolitan cities at the top of the selection menu
i have used this approach i there any alternative??
For metros:
List<Lst_City> lstCity= new List<Lst_City>();
lstCity = new BFCommon().getCities();
lstCity[0].CityID = 474;
lstCity[0].CityNM = "Mumbai";
lstCity[1].CityID = 199;
lstCity[1].CityNM = "Delhi";
lstCity[2].CityID = 165;
lstCity[2].CityNM = "Chennai";
lstCity[3].CityID = 384;
lstCity[3].CityNM = "Kolkata";
lstCity[4].CityID = 582;
lstCity[4].CityNM = "Pune";
lstCity[5].CityID = 71;
lstCity[5].CityNM = "Bangalore";
lstCity[6].CityID = 306;
lstCity[6].CityNM = "Hyderabad";
lstCity[7].CityID = 11;
lstCity[7].CityNM = "Ahmedabad";
Dropdown binding:
ddCities.DataSource = lstCity;
ddCities.DataTextField = "CityNM";
ddCities.DataValueField = "CityID";
ddCities.DataBind();
ddCities.Items.Insert(0, new ListItem("Select One", string.Empty));
Query:
public List<Lst_City> getCities()
{
List<Lst_City> temp = (from e in objCommonDataContext.Lst_Cities
where e.inUse == true
select e).ToList();
return temp;
}
and the combo box in designer
<div class="row">
<label>
City :</label>
<ajaxToolkit:ComboBox ID="ddCities" runat="server" AutoPostBack="False"
DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend"
CaseSensitive="False"
CssClass=""
ItemInsertLocation="Append" Width="380px"></ajaxToolkit:ComboBox>
am achieving the purpose but it doesn't seems ideal since hard coded and also entries are repeated
It seems from your code, all the cities are in a table.
My suggestion is:
Add a new column may be IsMetropolital
List<Lst_City> temp = (from e in objCommonDataContext.Lst_Cities
where e.inUse == true && e.IsMetropolital==false
select e).ToList();
List<Lst_MetroCity> tempMetro = (from e in objCommonDataContext.Lst_Cities
where e.inUse == true && e.IsMetropolital=true
select e).ToList();
List<Lst_City> lstCity= new List<Lst_City>();
foreach(var t in tempMetro)
{
// Add cities to the lstCity
}
foreach(var t in temp)
{
// Add cities to the lstCity
}
Now finally populate set the datasource of the dropdown with lstCity as you have done.
Related
I have trouble selecting rows in a dynamic way.
foreach (var item in _listBox.SelectedItems)
{
Treinen treinData = (Treinen)item;
Debug.WriteLine(treinData.Name);
}
Here i get the selected items name from a listbox with about 60 names in, depending on the selection I want to get the representing SQL data for those selected. I think this requires to append a dynamic .Where clause.
ObjectQuery<Fouten> fouten = eventsEntities.Foutens;
loadedData =
(from fout in fouten
where datumStart <= fout.Datum && datumEnd >= fout.Datum
.... here should where be extended in some way, for example:
&& foreach (var item in _listBox.SelectedItems)
{
Treinen treinData = (Treinen)item;
where fout.Treinen.Name == treinData.Name
}
orderby fout.Datum, fout.Time
select new
{
Datum = fout.Datum,
Time = fout.Time,
FoutCode = fout.FoutCode,
Omschrijving = fout.Omschrijving,
Teller = fout.Teller,
Module = fout.Module.ToUpper(),
FoutId = fout.FoutId,
TreinId = fout.TreinId
}).AsEnumerable().Select(x => new Fouten
{
Datum = x.Datum,
Time = x.Time,
FoutCode = x.FoutCode,
Omschrijving = x.Omschrijving,
Teller = x.Teller,
Module = x.Module,
FoutId = x.FoutId,
TreinId = x.TreinId
})
.ToList();
Obviously that doesn't work but hopefully it makes sense to what I'm trying to accomplish. Right now I get the data for all items instead of the selected ones. Anyone has a idea on how to solve?
I suppose your "Treinen" has an ID-Field (or Property) in it and since you have your fout.TreinId it is easier to go for the ID.
you can fill your ID-list before going into the query and then just check if your ID is in the List:
List<int> treinenIds = new List<int>();
foreach (var item in _listBox.SelectedItems)
treinenIds.Add(((Treinen)item).Id);
ObjectQuery<Fouten> fouten = eventsEntities.Foutens;
loadedData =
(from fout in fouten
where datumStart <= fout.Datum && datumEnd >= fout.Datum
//where-clause
where treinenIds.Contains(fout.TreinId)
orderby fout.Datum, fout.Time
select new
{
Datum = fout.Datum,
Time = fout.Time,
FoutCode = fout.FoutCode,
Omschrijving = fout.Omschrijving,
Teller = fout.Teller,
Module = fout.Module.ToUpper(),
FoutId = fout.FoutId,
TreinId = fout.TreinId
}).AsEnumerable().Select(x => new Fouten
{
Datum = x.Datum,
Time = x.Time,
FoutCode = x.FoutCode,
Omschrijving = x.Omschrijving,
Teller = x.Teller,
Module = x.Module,
FoutId = x.FoutId,
TreinId = x.TreinId
})
.ToList();
I'm not sure if the .Contains is supported in the query but if it is then this code should work for your Example.
I want to manage the data of gridview in different format like add sub heads
code of this data :
var data = (from r in data
join texp in totalexplist on r.SancPropId equals texp.AllSanchAmtId into list2
from l2 in list2.DefaultIfEmpty()
select new
{
Id = r.Id,
FYear = r.FYear,
SancPropId = r.SancPropId,
ExpSubHeadId = r.ExpSubHeadId,
ExpSubHeadName = r.ExpSubHeadName,
ProposedAmount = r.ProposedAmount,
SanctionedAmount = r.SanctionedAmount,
MonthYear = l2 == null ? "" : l2.MnthYear,
Expenditure = l2 == null ? 0 : l2.Exp,
}).ToList();
grd.DataSource = data;
grd.DataBind();
But i want to show this data in below given form
i have try this code but stuck in add to subheads
var monthwise = (from t in data
group t by new { t.MonthYear}
into grp
select new
{
grp.Key.MonthYear,
Data = grp.ToList()
}).ToList();
var allColumns = monthwise.SelectMany(d => d.Data.Select(s => s.ExpSubHeadName)).Distinct().ToList();
var datatable = new DataTable();
datatable.Columns.Add(new DataColumn("Month"));
allColumns.ForEach(c => datatable.Columns.Add(new DataColumn(c)));
Is anyone know how can i manage grid in this form ?
I want to add the content of the combobbox into my database, but it doesn't work. The content of my combobox is from the table 'Categories' who's joined with the table 'Products'. I've tried many things I have errors of conversion :
Here's my code :
Product p = new Product();
p.ProductName = txtNom.Text.Trim();
p.ProductDescription = txtDesc.Text.Trim();
p.ProductQuantityUsed = Convert.ToInt32(numQteUsed.Value);
p.ProductQuantityNew = Convert.ToInt32(numQteNew.Value);
p.CategoryID = cboCat.SelectedText.ToString();
db.Products.Add(p);
db.SaveChanges();
//Combobox
public void FillCbCategories()
{
SamsonEntities db = new SamsonEntities();
cboCat.Items.Clear();
var listCat = (from cats in db.Categories
select new CategoryDisplay()
{
CategoryID = cats.CategoryID,
CategoryName = cats.CategoryName
}).ToList();
for(var i=0;i<listCat.Count;i++)
{
cboCat.Items.Add(listCat[i]);
}
}
Judging by your comments, your combobox is not binded correctly to data you send to it.
You could try setting ValueMember and DisplayMember:
cboCat.ValueMember = "CategoryID";
cboCat.DisplayMember = "CategoryName";
in your method, like this:
public void FillCbCategories()
{
SamsonEntities db = new SamsonEntities();
cboCat.Items.Clear();
var listCat = (from cats in db.Categories
select new CategoryDisplay()
{
CategoryID = cats.CategoryID,
CategoryName = cats.CategoryName
}).ToList();
for(var i=0;i<listCat.Count;i++)
{
cboCat.Items.Add(listCat[i]);
}
cboCat.ValueMember = "CategoryID";
cboCat.DisplayMember = "CategoryName";
}
I have a need to display a list of departments and sub-departments. I am loading the list of departments properly.
At this time, I have:
var deparmentList = Departments.LoadList();
var departments = new List<ListItem>(
from department in departmentList
select new ListItem {
Text = department.Name,
Value = department.Id.ToString(),
IsSelected = department.IsActive
}
);
I now need to load the list of sub-departments. Each sub-department has an Id, DepartmentId, and Name. However, I only want to get the sub-departments associated with departments that are selected. Currently, I have the following:
var subDepartmentList = SubDepartment.LoadList();
var subdepartments = new List<ListItem>(
from subdepartment in subDepartmentList
// where ?
select new ListItem {
Text = subdepartment.Name,
Value = subdepartment.Id.ToString(),
IsSelected = false
}
);
I'm not sure how to do the join or filter between the two. How do I do this in LINQ?
var selectedDepartmentSubDepartments =
from dep in departments
join subDep in subDepartmentList
on dep.Value equals subDep.Id.ToString()
where dep.IsSelected
select new ListItem {
Text = subDep.Name,
Value = subDep.Id.ToString(),
IsSelected = false
};
var subdepartments = new List<ListItem>(selectedDepartmentSubDepartments);
You can add a where clause that checks if at least one associated and 'selected' department exists:
var subDepartmentList = SubDepartment.LoadList();
var subdepartments = new List<ListItem>(
from subdepartment in subDepartmentList
where departments.Any(x => x.IsSelected &&
x.Value == subdepartment.DepartmentId.ToString())
select new ListItem {
Text = subdepartment.Name,
Value = subdepartment.Id.ToString(),
IsSelected = false
}
);
I am developing the windows based application in which i need to bind the comboboxcolumns inside the datagridview with values from the [Attendance_type] table. And the datasource of this datagridview will be from [Employees table].
I am currently doing this using this code.
dgvEmployee.Columns.Clear();
dgvEmployee.AutoGenerateColumns = false;
DataGridViewTextBoxColumn branchcolumn = new DataGridViewTextBoxColumn();
branchcolumn.DataPropertyName = "Name";
branchcolumn.HeaderText = "Employee Name";
branchcolumn.Name = "Name";
branchcolumn.Width = 200;
dgvEmployee.Columns.Add(branchcolumn);
var metaattend = from Metaatt in dataDC.Metaattend
where Metaatt.Status == true
orderby Metaatt.Metaname
select Metaatt;
List<Metaattend> obj_ma = new List<Metaattend>();
obj_ma = metaattend.ToList();
var Empvar = from Emp in dataDC.Employees
join dept in dataDC.Dept on Emp.Deptid equals dept.Id
join branch in dataDC.Branch on Emp.Branchid equals branch.Id
where Emp.Status == true & Emp.Name.Contains(txtbranch.Text)
& (Emp.Deptid == Convert.ToInt64(cmbDept.SelectedValue) | cmbDept.SelectedValue.ToString() == "0")
& (Emp.Branchid == Convert.ToInt64(cmbBranch.SelectedValue) | cmbBranch.SelectedValue.ToString() == "0")
orderby Emp.Name
select new { Emp.Id, Emp.Name };
DataTable dt_employee = new DataTable();
using (clsGeneral obj_gen = new clsGeneral())
{
dt_employee = obj_gen.LINQToDataTable(Empvar);
}
dgvEmployee.DataSource = dt_employee;
string[] datemonth = cmbMonth.Text.Split('-');
int i = DateTime.DaysInMonth(Convert.ToInt32(datemonth[1]), GetMonthNo(datemonth[0]));
for (int j = 0; j < i; j++)
{
DataGridViewComboBoxColumn daycomboColumn = new DataGridViewComboBoxColumn();
daycomboColumn.HeaderText = (j + 1).ToString();
daycomboColumn.Width = 50;
daycomboColumn.DataSource = obj_ma;
daycomboColumn.DisplayMember = "Metaname";
daycomboColumn.ValueMember = "Id";
dgvEmployee.Columns.Add(daycomboColumn);
}
Using this the code is executed successfully, but the form does not shows me any record in the column for combobox inside the datagridview.
This is the view of my form, when i click on the comboboxes then also it does not shows me anything.
I have seen many post about this but none was helpfull for me. As that all shows me the same thing that i have done.
Please help be with about where i have done the mistake.
Thanks
Enable Row Editing property for the datagridview was set to false. so it was not showing the Data inside the combobox.