Grid data setup control - c#

I am struggling with the grid view control and it seems I miss something as I am
not able to show data in it. I am trying the bind the control to a list of some
objects.
Can you please walk me through and explain how I need to setup the control?

Assuming a lot here since I don't have any code to go off of....
Given a List<GridDataModel> you would set your DataSource property to it and call DataBind method of your GridView.
var myData = GetMyData(); //this function builds the List<GridDataModel>
MyGrid.DataSource = myData;
MyGrid.DataBind();
A more complete example.

Related

C# How can I display a selectable list of objects displaying select attributes but on select returns the object

I have a form that a user can fill out to add a new entry to the DB or update an existing one. I need a view that can display the objects number and description and can be selected by a user to prepopulate the form with that objects data for updating.
I tried a data grid view and I got my objects displaying but it shows ALL objects attributes. I only want two of them to display. Also data grid views seem to be selectable by cell not row.
This is how I bound my data. Is there a way to make it selectable by row and give me back the object? Or only display some attributes?
var source = new BindingSource();
List<RollingStock> rollingStockList = RollingStockDAO.GetRollingStocks();
source.DataSource = rollingStockList;
dgvInventoryList.DataSource = source;
I'm still learning to code and Im not super familiar with C# yet. Any help is appreciated!
I tried a data grid view and I got my objects displaying but it shows
ALL objects attributes. I only want two of them to display. Also data
grid views seem to be selectable by cell not row.
those are both properties you need to specify.
to stop the other columns from being created use:
dgvInventoryList.AutoGenerateColumns = false;
to make the entire row get selected instead of just the cell use:
dgvInventoryList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
im sure they are also in the property grid somewhere.
Is there a way ... give me back the object?
to get the object bound to the row use:
DataGridViewRow.DataBoundItem
then cast it to your object:
MyObject rowObject = dgvInventoryList.Rows[0].DataBoundItem as MyObject;
to get the object when the row is clicked, you can handle the Cell.Click event:
private void dgvInventoryList_CellClick(object sender, DataGridViewCellEventArgs e)
{
MyObject rowObject = dgvInventoryList.Rows[e.RowIndex].DataBoundItem as MyObject;
}
there is also a HeaderCell.Click event and a few others. you should be able to find one to suit your needs.

C# Display Only Specific Items from a Data Source in CheckBoxList

I'm binding a CheckBoxList in a Winform to a data source. However, instead of displaying all items from that source in the list, I'd like to be able to pick out only the ones I really want. I'm stumped on how to do that, or if it can even be done.
Right now the CheckBoxList is set-up and bound to a data source like so:
((ListBox)this.cblFormType).DataSource = formTypeData;
((ListBox)this.cblFormType).DisplayMember = "ProductClassName";
((ListBox)this.cblFormType).ValueMember = "ProductClass";
Any help would be much appreciated.

Add item update listbox

So I have a databound listbox bound to a list produced from my entity:
myListbox.Datasource = myEntity.ToList();
That works fine. My question is, what is the 'correct' way to add a new element to the entity and have it reflected in my listbox?
Currently, I do this:
myEntity.Add(newItem);
myListbox.Datasource = myEntity.ToList();
Surely there is a better way than resetting the datasource each time?
try this, DataBind() will binds a data source to the invoked server control and all its child controls.
myListbox.DataBind()
Please try this:
myListbox.ResetBindings();
See https://msdn.microsoft.com/en-us/library/system.windows.forms.control.resetbindings%28v=vs.110%29.aspx

c# generics. Trying to set columns generically for a grid control

I need some help on how to set the list of columns for a a grid control generically. I have a 3rd party library with the following method signature I want to use:
public GridControl SetColumns<T>(Action<GridColumnModelList<T>> initCols)
In my Controller on the action method I get a generic model the contains the list of columns I want displaying:
var gridprofile = new GridProfile<SiteVisitSearchGridViewModel>(gridProfileid);
I now want to create the grid control and apply my columns to it but dont know how to do this:
GridControl gc = new GridControl();
gc.SetColumns<SiteVisitSearchGridViewModel>(gridprofile.Columns);
help please
thanks
Andy
you could try something(it may not compile - I don't have that library installed) like this:
gc.SetColumns<SiteVisitSearchGridViewModel>(columns =>
{
foreach(col in gridprofile.Columns)
columns.Add(x => new GridColumnModel(col.Name));
});

Set a datalist.datasource from inside a usercontrol - asp.net c#

I have a usercontrol that contains a datalist, and I want to set the datasource of the datalist to different things depending on what page the usercontrol is on.
So, I think what I need to do is expose a public property of the datalist that will get the datasource and set it, like so:
public datasource UserDataSource
{
get { return DataList1.DataSource; }
set { DataList1.DataSource = value; }
}
but the above obviously doesn't work. I'd then call it like this:
MyUserControl.UserDataSource = datasourcename;
and then somehow databind the datalist inside the usercontrol.
Obviously I'm kind of out of my element here, but hopefully I can get this working. Thanks for any help.
You need to use find control method to find your datalist first and then assign datasource like...
DataList dl = (DataList)yourLoadedusercontrol.FindControl("yourDatalist");
dl.DataSource = yourdatasource;
I know you have already accepted an answer, but I feel I must add my thoughts:
Your original idea was correct - you just needed to call the databind method of your datalist after setting the datasource. I truly do not feel that doing the above mentioned method is the best way. You should really just have a method or writeonly property (like you do) that takes a possible IList or IEnumerable of your custom object and binds it directly to your datalist. Your page or control that contains this user control should not be aware of your type of data control. If you change it from a Datalist to a Repeater or a GridView, you have to change it everywhere you bind to your user control.
Example:
IList<CustomClass> results = new List<CustomClass>(); //you would load your collection from your database
this.myUserControl.LoadData(results);
In your user control:
public void LoadData(IList<CustomClass> data){
this.datalist1.datasource = data;
this.datalist1.databind();
}

Categories