Populate Combo box using LINQ query - c#

I'm trying to populate a combo box with data resulting from a LINQ query on a DataSet. The problem is, nothing is showing up in the combobox. At all.
var digits =
(from digit in DDDataSet.Digits.AsEnumerable()
where (!digit.pressed)
select new {DigitList = digit.name});
cboDigits.DataSource = digits.ToList();
cboDigits.DisplayMember = "DigitList";
Any ideas?

I think you need to:
cboDigits.DataBind();
after you set the DataSource and DisplayMember.
Hope this helps

just try this
var stuff = dg.Stuffs.Where(c=> c.admin !=1).ToList();
for (int i = 0; i < stuff.Count; i++)
{
string test = stuff.ElementAt(i).Name;
comboBox1.Items.Add(test);
}

Most controls do data binding automatically, which means that you typically do not need to call the method DataBind explicitly.
Linq when using ToList() won't be deferred, so all the data is computed. But, for combobox, it seems that you still need to use DataBind().
If you are using Winform, you do not need DataBind to be called. If you are using Asp.Net you will need to.

Related

How to remove row from datagrid in asp.net

I have a datagrid dgCompanies declare like this:
// bind the data to the datagrid
dgCompanies.PageSize = pageSize;
dgCompanies.DataSource = rdr;
Then I need to check the records inside this datagrid:
int j = 0;
foreach (DataGridItem item in dgCompanies.Items)
{
HtmlGenericControl name = (HtmlGenericControl)item.Cells[j].FindControl("SpanTitle");
string drstring = name.InnerHtml.Trim();
if (checkingfunction(drstring))
{
//do removing record from datagrid.
I tried this: item.Cells.Remove(item.Cells[j]); but the result still there, don't see anything removed!
}
j = j + 1;
}
dgCompanies.DataBind();
How could I remove that record from datagrid dgCompanies when if condition is satisfy ?
I agree with #Andrei, it would be best to just not return that data. Otherwise you're returning and churning through data that is unnecessary. However, if in the context of you can't filter the data up front, I suppose you could add a css class "donotshow" to your rows (see How do I specify CSS classes for specific rows in a GridView?).
Then using jquery use
$('.donotshow').hide();
Again, in this regard, you are still returning all that HTML to the browser, so that will make your page size larger than necessary. Additionally, using this method could screw up pagination if you using it (example, if it says "rows 1-10", but 5 rows are hidden, then that will look goofy).
Hope this helps

How to select an Item from asp:Dropdownlist on Page load from Code Behind C#?

I tried so many articles around, like below to get my task done, but didn't work as I always ends up with an NullReferenceException, I have bound a database table column to the Dropdown list, on page load i want to select an item based on the value from database which is one of those listed items. Please help me.
txt_examtype.DataSource = dt;//txt_examtype is the dropdownlist
txt_examtype.DataTextField = "ExamTypeName";
txt_examtype.DataValueField = "ExamTypeName";
txt_examtype.DataBind();
String examtype = dt.Rows[0]["ExamType"].ToString().Trim();
ListItem myitem = txt_examtype.Items.FindByValue(examtype);
txt_examtype.SelectedValue = myitem.Value;
try this code
txt_examtype.SelectedValue = dt.Rows[0]["ExamType"].ToString()
You should set SelectedIndex instead of SelectedValue. This is safe to use:
txt_examtype.SelectedIndex = txt_examtype.Items.IndexOf(txt_examtype.Items.FindByValue(examtype));

distinct value from a column in a grid using linq

I am using the below query to find the the distinct value count from a dataset column. How to find similarly to a grid column.
var distinctRows = (from DataRow dRow in _ds.Tables[0].Rows
select dRow["colName"]).Distinct();
Ok..I'm still not sure why you want to do it without requerying the DataSource, but here's one way that might point you in the right direction. gv1 is the ID of your GridView, and for demonstration purposes I'll use the first column:
string[] rowValues = new string[gv1.Rows.Count];
for (int i = 0; i < gv1.Rows.Count; i++)
{
rowValues[i] = gv1.Rows[i].Cells[0].Text;
}
var distinctRows = (from r in rowValues
select r).Distinct();
This of course assumes that it's one cell per column, which may be a false (or at least bad) assumption.
UPDATE
Just saw this Can't seem to use Linq with ASP.Net Navigation menu answered by Jon Skeet, and think it might apply to the issue here.
var distinctRows = (from GridViewRow r in gv1.Rows
select r.Cells[0].Text).Distinct();
Courtesy of Jon's answer, to use LINQ in this case you need to Cast to IEnumerable (as I'm willing to bet the GridViewRowsCollection doesn't implement IEnumerable) by explicitly specifying the item, as above.
Your grid is probably bound to a data source in which case it makes more sense to use the linq query against the data source rather than the grid itself

WebForms Passing CheckboxList Values To List<T>

I have a GridView which I have a List bound to - I want to be able to allow filter of the data based on multiple CheckBoxLists.
For arguments sake let's say I have a List of Jobs, and I want to filter the jobs by code - So I would have a CheckBoxList like
C#
ASP.NET
php
F#
etc..
If someone selects multiple codes, how do you pass the values into a List to rebind to the GridView? Is there a way to pass values as an array? Or maybe a comma seperated string?
Any examples (I'm a C# boy) would be greatly appreciated, and I hope I have explained it properly :S
use an ObservableCollection<T> . it automatically allows the gridview to "observe" that the underlying datasource has changed and thus update itself.
wherever you do your filtering for the gridview you have to build the list manually before you filter.
var languages = new List<string>();
foreach (ListItem item in cblLanguages.Items)
{
if (item.Selected)
{
languages.Add(item.Value);
}
}
then when you filter you can do something like (example using linq2sql)
var jobs = db.Jobs.Where(x => langauges.Contains(x.LanguageCode));
gvJobs.DataSource = jobs;
gvJobs.DataBind();
I'm not sure I completely understand your question. But I often do the following to get ListItems into a form queryable via LINQ to objects:
var items = cblLanguages.Items.Cast<ListItem>();
// Selected Items
var selectedItems = items.Where(li => li.Selected);
// Item's containing 'C'
var itemsWithC = items.Where(li => li.Text.Contains("C"));
// Values between 2 and 5
var itemsBetween2And5 = from li in items
let v = Convert.ToInt32(li.Value)
where 2 <= v && v <= 5
select li;

DataGrid: Dyanmically adding rows

I am new to windows application. I need to add rows in the DataGrid dynamically which has person data. when i do the following is see only the last person in the last row. i see rows populating but with no data. If i do a break on the first fetch i do get the right one. But something is wrong. Any ideas
foreach (var p in personList)
{
gvAdminSummary.Rows.Add(new DataGridViewRow());
gvAdminSummary.Rows[gvAdminSummary.Rows.Count-1].Cells[0].Value = p.FName;
gvAdminSummary.Rows[gvAdminSummary.Rows.Count - 1].Cells[1].Value = p.LName;
gvAdminSummary.Rows[gvAdminSummary.Rows.Count - 1].Cells[2].Value = p.PNo;
}
The DataGridRowView.Add method accepts string arrays:
gvAdminSummary.Rows.Add( { p.FName, p.LName, p.PNo });
Likely, though, there's a better solution for you in binding the grid directly to your person list.
This may not be the right approach. Create a BindingSource and bind a collection of your objects to it. Then bind the BindingSource to the Grid's data source. Make sure your objects implement INotifyPropertyChanged. This way, whenever, you add an object to the collection, or change a property within your object, it'll automatically reflect in the grid.
I don't know about DataGridView, but if you want to stick to inserting data into the control directly, why not use ListView instead? It has an API more suited to your current needs or way of doing things.
Either
gvAdminSummary.Datasource = persons;
gvAdminSummary.databind();
Or
foreach (var p in personList)
{
DataGridViewRow dr = new DataGridViewRow();
dr.cells.add(new datagridcell()) etc.. populate cells
gvAdminSummary.Rows.add(dr);
}

Categories

Resources