Update DataGridView while running test - c#

I am currently writing a piece of functional test (FCT) software used for testing PCBAs. The main interface between the user has a DataGridView that will be used to display all information pertaining to the functional test. I'm also transitioning from LABView to C# to develop these so I'm still learning a bit.
What I want to do at the start of the application is to show a 5 column x 100 row table for the data. I chose 100 rows to make the table fill up the window (it also looks a little better this way too).
The issue I'm having is when I try to add data to the same row. Note that the first set of data put in the grid will only be in two columns, while data added later on will span all five columns. Here's what I'm trying now to update the datagrid:
mydatagrid.RowCount = 100; //Put
int rowIndex; //variable for tracking where I'm placing data on the table
//Do some stuff that doesn't involve my datagrid
mydatagrid.Rows[rowIndex].Cells[0].Value = someData; //This line executes fine
mydatagrid.Rows[rowIndex].Cells[1].Value = someOtherData; //This throws an exception
//Do more stuff that doesn't involve my datagrid
Another method to update the table I tried was
mydatagrid.Rows.Add(somedata, someotherdata);
While this works nicely, it puts the data at the end of the table, rather than at the top.
Also, later on in the app will also need to something like this:
activaterelays.clicketyclackety();
makemeasurement();
updatedatagrid(); // This function will put data in all five columns
Maybe I am missing something trivial with the setup, at it's my first go at a DataGridView with Windows Forms.

To use indexing in rows and cells in a DatagridView, you must fill it with data first.
Try something like this:
public class MyData {
public string column1{ get; set;}
public string column2{ get; set;}
public string column3{ get; set;}
public string column4{ get; set;}
public string column5{ get; set;}
}
var data = new List<MyData>() {...}// You must insert the 100 items
//Loads the data to DatagridView, after this line you will see the DatagridView populated.
mydatagrid.DataSource = data;
//Now you can manipulate it
mydatagrid.Rows[rowIndex].Cells[0].Value = someData;
mydatagrid.Rows[rowIndex].Cells[1].Value = otherdata;

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.

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.

How to save CheckBoxRowSelect in Devexpress GridView to class object

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.

How to sort numbers in a table?

I've got a rather small table containing two columns - one is string and one is supposed to be numbers (I know, everything in a table is just "object").
Is there any way to convert the objects into for example Int or Double to be able to actually sort it in a reasonable way?
The sources for that table are two lists. I could sort the number list, but the data wouldn't match anymore.
Other idea would be to transfer the content of the lists into a multidimensional array and to sort that one but I'd prefer a solution to sort that table directly.
Anyone got an idea?
Rather than working with two lists, merge them:
public class MyRow { // rename me
public int Element { get; set; }
public int Appearances { get; set; }
}
....
var rows = new List<MyRow>();
for(...) {// loop over the two lists...
rows.Add(new MyRow {
Element =..., Appearances = ... });
}
rows.Sort((x,y) => y.Appearances.CompareTo(x.Appearances));
Then either just loop and create cells, or (easier) just set rows as the data-source for the table and let it bind itself.

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