How to split cell of a grid into columns winforms devexpress - c#

I would like learn something.
I have a GridControl which has a gridview inside with some columns. And there's one column lets name "jobs". in my DataList(which i'm populating rows with) i have another list which will hold some infos about jobs and i want to show them in one column. But there's the problem. I want to split the cell of "jobs" . how can i do that?
the model and the list is like below:
//this is my job class
public class JobInfo{
public string Name;
public bool IsDone;
}
//this is my work class
public class Work{
public string Company;
public string Personel;
public List<JobInfo> Jobs;
}
in my cs file I will hold a List<Work> object. I can bound it to grid view but i want to show job info like that cell contains 4 or 5 cells or how many jobs that list has.
I tried some paint to be as clear as much
http://imgur.com/eEB9krO
Any help appreciated. Thanks

You can not have columns inside columns in a GridView.
How about just adding it into one cell like this?
private void gridView_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
if (e.Column.FieldName == "Jobs")
{
var jobs = e.Value as List<JobInfo>;
e.DisplayText = jobs != null ?
string.Join(" | ", jobs.Select(j => string.Format("Name: {0}, Is Done: {1}", j.Name, j.IsDone))) :
string.Empty;
}
}

There is no way to do exactly what you need. But maybe it is an option to use a repositoryItem. You could use a popupcontaineredit. Then you build a popupcontainercontrol with a new Grid. If someone click a row or on the dropdown of your popupcontaineredit you can popup your popupcontainercontrol and populate the list of jobs in the uppoping grid.
I hope you understand what i mean.
regards.

Related

Getting an IEnumerable of all values in a certain column

I'm currently working with the Atata framework and using a Kendo grid. What I'm trying to achieve is getting an IEnumerable or a collection containing all the values in a column. The kendo grid is defined as below:
public KendoGrid<TransactionGroupsRow, _> TransactionGroupsGrid { get; private set; }
public class TransactionGroupsRow : KendoGridRow<_>
{
[FindByColumnHeader("Group Name")]
public Link<_> GroupName { get; private set; }
}
I not only want a collection of all the links GroupName, I specifically want the text component of them i.e. what the link would read like on the screen.
So in this example I would want an IEnumerable containing "002999" and "003999".
Let me know if I need to provide additional detail. Thanks in advance!
There are 2 ways to do that.
Use SelectData method. It works quite fast for list of items with count <= 50.
// Just get all items:
IEnumerable<string> groupNames = page.TransactionGroupsGrid.Rows
.SelectData(x => x.GroupName.Content.Value).Value;
// Assert items:
page.TransactionGroupsGrid.Rows
.SelectData(x => x.GroupName.Content.Value).Should.Contain("002999", "003999");
The faster way is using SelectContentsByExtraXPath method. This one will work very fast even for a table with 1000 rows. But you need to specify a relative XPath to the table cell td element inside a row tr element. This can also be extracted to the page object class as a method.
// Just get all items:
IEnumerable<string> groupNames = page.TransactionGroupsGrid.Rows
.SelectContentsByExtraXPath("td[1]", "Group Names").Value;
// Assert items:
page.TransactionGroupsGrid.Rows
.SelectContentsByExtraXPath("td[1]", "Group Names").Should.Contain("002999", "003999");
You can also check Performance Practices for ControlList Atata sample project.

How to update attribute value from object in a ListView

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();

filter datagridview using another datagridview

I have a text file with size about 10-20 MB.
I want to perfom this options.
1) read the text file and split.
2) move all the data in each line into a datagrid view.
3) create an addition datagridview. that the user can define which line can be visible and which not per a values of 1 column (filter).
I have written a code however however it take a long time.
reading to the first datagrid view the text takes about 40 secound.
when I tried to filter it takes about also 40-50 secound till the user filter it,
is there a way to reduce the time? what is the best way to do this kind of thing? and does using a datasource can be helpful?
Thanks,
I think its better to use linq.
Read your file like this and you have a list of file lines.
var lst = System.IO.File.ReadAllLines(FilePath).ToList();
Now you can filter your list by linq statements.
And you can simply show the list in your datagrid.
If you want to show all data in datagrid, it takes time.
You can also create a class for that purpose
public class MyLine
{
public string Line {get; set; }
public bool IsVisible { get; set; }
}
Then you can read the file and have a list or your class, like this:
var lst = System.IO.File.ReadAllLines("")
.Select(x => new MyLine() { Line = x, IsVisible = true });
Then for getting visible ones write this query:
var Visibles = lst.Where(x => x.IsVisible);
Hope it helps.

Sort a Grid when after loading all data in the grid

I have a grid with 4 columns 'Id' 'Name' ' Address' 'Age'
I getting all the Id's in the list and binding to the grid. Now in Grid_itemDataBound event depending upon the Id I am getting the Name, Address Age for that particular row.
now I want to sort the grid depending on the Name.
How I can Sort depending on the Name which was binded to grid in Item databound
Please do Needful
my Code
grdEmployee.DataSource = lstEmployee.OrderBy(x => x.Id).ToList();
protected void grdEmployee_ItemDataBound(object sender, EventArgs e)
{
Label lblname = (Label)e.Row.FindControl("lblName");
Label lblAge = (Label)e.Row.FindControl("lblAge");
Label lblId = (Label)e.Row.FindControl("lblId");
lblName.Text = GetName(lblId.Text);
lblAge.Text = GetAge(lblId.Text);
}
Note: this is sample code I have manually written in this editor. please Ignore faults in the code and please understand my scenario.
I think, it would be better if you get all the data at once, then doing it in parts in each call to ItemDataBound. This way instead of just a list of ID you will have a DataTable or a List of Employees. And then you can sort this DataTable or List the way you like - using DefaultView.Sort (for DataTable) or LINQ OrderBy.

ListView vs. ListBox for multiple column usage

I am currently struggling with the GUI of my application. I have a hard time figuring out whether the ListBox or ListView is more "suitable" for multi-column representation of data.
I prefer "clean" code that is not too confusing to figure out, as spaghetti code and hacking methods can lead to confusion.
How do both ListBox and ListView handle multiple columns?
There's certainly nothing wrong with a DataGridView in this scenario.
Sample:
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
}
A function to load the data to the DataGridView
private void LoadData()
{
List<Car> cars = new List<Car>()
{
new Car() { Make = "Subaru", Model = "Impreza", Year = 2005 },
new Car() { Make = "Ford", Model = "Mustang", Year = 1984 }
};
dataGridView1.DataSource = cars;
}
Of course, from here things can get more complicated, but if you simply want to display data in a tabular fashion... it's pretty simple.
Check this out
https://stackoverflow.com/a/227355/988830
Though listbox is used for single column and listview is used for mutlicolumn, the answer is it all depends.
Sometimes you may need multicolumn list where you need to add different types of children. You cannot bind them using listview so its better to use listbox in such scenarios. But if you want to sort them by using header, do use listview because it is simple.
In conclusion, I would say if you just have multi column data and nothing more better to use listview else if you want to do fancy stuff like buttons, treeview, expander etc. ListBox is really cool.
Thanks,
Omkar
A DataGridView is nice if you want to be able to edit data straight from the grid, like a spreadsheet. A listview in detail mode is great for simple presentation of lists of data columns. A DataGridView will also be easier to sort, as far as I know.
Generally I do something like this:
private void UpdateListView()
{
mListView.Items.Clear();
foreach (Item item in mItems)
{
ListViewItem listViewItem =
new ListViewItem(item.Value1.ToString()) { Tag = item; }
listViewItem.SubItems.Add(item.Value2.ToString());
listViewItem.SubItems.Add(item.Value3.ToString());
mListView.Items.Add(listViewItem);
}
}
The columns will need to have been defined in the designer, including column header text and column widths.
With the Tag = item; part you will be able to access the selected object with:
if (mListView.SelectedIndices.Count <= 0)
return;
Item selectedItem = mListView.SelectedItems[0].Tag as Item;
if (selectedItem == null)
return;
// do something with selectedItem
ListView is much better for multi-column representation of data. However it seems to get more complicated/ugly code than a simple ListBox.
Still its much better for many reasons, resizeable columns and all that.
I don't think ListBox has multiple columns so you'd have to hack something ugly in.
http://www.xtremedotnettalk.com/showthread.php?t=93443

Categories

Resources