I've simple class called "Order".
class Order {
public string ID { get;set;}
public string Something {get;set;}
.... more fields...
}
I then assign list of orders into DataSource of GridControl.
List <Order> ListOfOrders = new Order();
gridControl.DataSource = ListOfOrders;
I've added a CheckBoxRowSelect option in GUI so that user can choose rows by selecting the checkbox. What value do I need to add to class Order so that any checkbox change in that GridControl gets updated in instant in it's own field so that the ListOfOrders is always up to date and has that checkbox value stored so it can be processed?
I've made similar thing with ComboBox inside one of the columns and it seems to work instantly without any additional change on my part.
var columnKurier = view.Columns.AddField("Courier");
columnKurier.ColumnEdit = riCombo;
columnKurier.VisibleIndex = 0;
columnKurier.OptionsColumn.AllowFocus = true;
I simply had to add Courier field into Order class and that's it. But i don't know which "field name" should it be for checkbox and whether it will get the same behaviour as for the ComboBox.
Add in your object order :
public Boolean Mark{get;set;}
and in your your form contructor :
InitializeComponent();
new GridCheckMarksSelection(gridControl);
i have atach a class GridCheckMarksSelection you must add it in your project.
MarkClass
I hop that you want
You can not add the option CheckBoxRowSelect to GridControl, and just use a bool field in your Class Order, it should be displayed as checkbox in GridControl.
Related
I have two files. One file with a class called Wood. I have created some objects with this class. These Objects include the attribute WoodType and a few others. I filled the objects into an array called woodObjects. The second file is a Form where I have a ListView.
So my goal was, to add all objects with their attributes to the ListView. I did it like that:
String[] row = { (listView1.Items.Count + 1).ToString(), WoodType, condition, isDry};
ListViewItem item = new ListViewItem(row);
listView1.Items.Add(item);
Success.
Now I have to update the attribute WoodType of one object in the ListView. I created a button "change Type" for that. I thought of the user clicking the row where the object ist he wants to change, and then clicking the button "change Type" to change WoodType it.
To realize this, I wanted to take the index of the row, and than changing the value of "WoodType" with its set Accessor with something like the following:
woodObjects[indexOfRowFromListView].WoodType = "Oak Wood";
Yes, I know, this would never work like that. So my questions are: How can I select the correct index and how do I change the value of the attribute?
*I also thought of deleting the objects, but saving its values so I can create a new objects with the same values except WoodType.
I appreciate every help!
I think the cleanest solution to your problem would involve writing a new ListViewItem implementation. Something like this:
public class WoodObjectListItem : System.Windows.Forms.ListViewItem
{
public Wood WoodObject { get; }
public WoodObjectListItem(int rowNumber, Wood woodObject)
: base(new string[] { rowNumber.ToString(), woodObject.WoodType, woodObject.Condition, woodObject.IsDry })
{
WoodObject = woodObject;
}
public void ChangeType()
{
WoodObject.WoodType = "Oak Wood";
}
}
Then you could add your woodObjects to the list view like:
listView1.Items.Add(new WoodObjectListItem(listView1.Items.Count + 1, woodObject));
And when the button is clicked you could do:
if (listView1.SelectedItem is WoodObjectListItem woodObjectListItem)
woodObjectListItem.ChangeType();
I'm using datagrid as the temporary list that until I decide to insert in the database all the data remain in datagrid. I'm adding functions like adding new records, deleting or editing. but I can not change selectedrow
my code is
gridlist.Items.Add(new { num_ins = num_ins.Text, dat_ins = DateTime.Now.ToShortDateString()} --> and many other value
and for delete
var selectedItem = gridlist.SelectedItem;
if (selectedItem != null)
{
gridlist.Items.Remove(selectedItem);
}
i want to make something like this
gridlist.Columns[0].gridlist.Items[1]= "my value";
I wanted to know if it is possible to do this directly from the datagrid as a removal or creation of new records
There are two possibilities.
If you always just add an anonymous type to the Items collection, then it is not possible no update the individual column values. You can however, update the entire row
gridlist.Items[0] = new { num_ins = 1, dat_ins = DateTime.Now};
If you could create a class/structure with public properties for the items being added, then you can update the individual properties of the items.
public class temp
{
public int num_ins {get;set;}
public DateTime dat_ins {get;set;}
}
((gridlist.Items[0] as temp)).num_ins = 3;
In my WPF app, i have a Window(all code) that contains DataGrid. There's my DataGrid binding:
using (var db = new CompanyEntities())
{
var stocks = db.Stock;
var query = from s in stocks
select new { s.Id_Product, s.Quantity };
dataGrid.ItemsSource = query.ToList();
}
I want to read cell that is selected in DataGrid. I tried to read it using DataGrid.SelectedItem, but to get to the item i have to cast it to some type. I can't use Stock class, because my DataGrid cells contains 2 out of 3 fields of Stock. So i made another class to represent DataGrid cell:
class TableItem
{
public int Id_Product;
public int? Quantity;
}
And tried to cast it like this:
TableItem x = (TableItem)dataGrid.SelectedItem;
But i get InvalidCastException from type:
'<>f__AnonymousType02[System.Int32,System.Nullable1[System.Int32]]'
to my TableItem type.
So how should i get selected cell of my dataGrid?
My walkaround:
In my CompanyEntites constructor i set:
this.Configuration.ProxyCreationEnabled = false;
And now i can cast Stock s = (Stock)dataGrid.SelectedItem;without an exception, before switching ProxyCreation off, my SelectedItem was DynamicProxy.Stock~~ type.
Change your query like so:
var query = from s in stocks
select new TableItem { Id_Product = s.Id_Product, Quantity = s.Quantity };
And then cast SelectedItem to TableItem, now that it actually IS one. In C#, just because it looks like another class, doesn't mean it is that other class.
Alternatively, don't write a new class just because you happen not to need one property of the existing class. Just use the existing class:
// Don't think you need to call ToList() here
dataGrid.ItemsSource = db.Stock;
...and cast SelectedItem to whatever that is.
my DataGrid cells contains 2 out of 3 fields of Stock:
So what? Is the problem that you get an extra column? Define the columns explicitly, just the ones you need.
I have a ListView of this type:
<ListView x:Name="LView">
<ListView.View>
<GridView x:Name="GView"></GridView>
</ListView.View>
</ListView>
Initially I don't know how many columns there will be in my GridView, and I don't know what is the type of a single cell.
I need to have the possibility to add at run-time new columns and especially new rows whose cells contain different objects.
ListView has the property Items, so if I have a single column of string and Iwant to add a row i write:
LView.Items.Add("my string");
Now i want to add a new column:
GView.Columns.Add(new GridViewColumn());
A new column is added but the second cell of the first row contains the string "my string", why? I don't want this behavior but a new empty cell.
And especially if I want to add a new row with the first cell as String and the second cell as CheckBox what I have to do?
I tried:
LView.Items.Add("my second string", new CheckBox());
But, obviously, it does not work.
I tried to add a new Object:
public class obj
{
public string MyString{ get; set; }
public CheckBox MyCBox { get; set; }
}
LView.Items.Add(new obj());
But it dows not work because it display a string that contains (Namespace "+ obj"), and I don't know how many columns there will be, so I can't know how the obj class have to be initially.
What I have to do to solve my problems? Thanks.
I think you mix data items with ui elements
LView.Items.Add("my string");
will be presented as a textelement using default template
If you havent defined any data templates and want to explicitly add ui elements try this instead:
LView.Items.Add(new TextBlock { Text = "My string"});
However, I think GridView is suppoused to be populated with data (along with power of data bindings), not explicitly ui elements. Column Template should define how data is presented (in your case an checkbox or textelement).
See example http://www.c-sharpcorner.com/uploadfile/mahesh/gridview-in-wpf/
My project display's program names from the database into dropdownlist and each program has a ID. I want to display both name and id in the dropdownlist so that they can be differenciated from each other.
eg: 'california lifeline (CLA)'
where 'california lifeline' is the name of the program and the id is 'CLA'. I have created a stored procedure which displays data based on the program ID.
This the code of my dropdownlist control.
private void LoadProgramName()
{
_drp_program = (DropDownList)Page.FindControl("bodyuc$drp_program");
dsprg = rProxy.GlobalFetchFromDB(strCountyName, "DBO.oea_sp_get_onoff_programNames");
_drp_program.DataSource = dsprg;
_drp_program.DataTextField = "PROG_NAME";
_drp_program.DataValueField = "PROGRAM_ID";
_drp_program.DataBind(); ;
ListItem lst_prog = new ListItem();
lst_prog.Value = "";
lst_prog.Text = "--Select One--";
_drp_program.Items.Insert(0, lst_prog);
_drp_program.Items.Insert(1, "ALL");
}
BTW, the dropdown is a part of pagecontrol.
Help is Appriceated.
If your GlobalFetchFromDB() returns instances of an object which you can "extend" as a partial class, then I'd add a "helper" property to the class that formats your displayed string.
One advantage to this is that the format can be dependent on the values of other properties in the instance.
For example:
public partial class ProgramItem
{
public string DisplayName { get { return PROG_NAME.ToUpper(); } }
}
Then use _drp_program.DataTextField = "DisplayName";
An alternative is to construct a class that encapsulates the returned items and adds the DisplayName as a decorator.
Instead of binding _drp_program to dsprg, loop through the results in dsprg and add each item individually.
foreach(dsprgObject in dsprg)
{
_drp_program.Items.add(new ListItem(dsprgObject .ColA + " " + dsprgObject .ColB, dsprgObject.PROGRAM_ID));
}
You can try
_drp_program.DataSource = from item in dsprg
select new
{
PROG_NAME = string.Format("{0}({1})", item.PROG_NAME, item.PROGRAM_ID) ,
PROGRAM_ID = item.PROGRAM_ID
};
instead of
_drp_program.DataSource = dsprg;
Usually the fastest way is to concatenate ID and NAME in an SQL query (but you're using an sp so that's a bit difficult). Also, since you binding your control in code, you can concatenate ID and Name in code and populate your dropdownlist manually (either by calling Items.Insert or by binding your dropdown to a collection which members have a field/property that contains the concatenated value). AFAIK, you can only specify a field/property name for DataTextField and DataValueField, no expressions etc...