Show columns in specific display order by default in GridView - c#

I'm trying to figure out the easiest way to have my columns start in a default order(left to right) for all users.
I tried setting them in the IList<T> that I use to populate the DataSource but that doesn't work.
Do I have any options besides setting each column manually following the instructions on this page -->Reordering Columns

Take a look at the following documentation.
http://www.telerik.com/help/winforms/gridview-populating-with-data-tips-when-binding-to-custom-collections.html
You can pretty much define the way you want the columns to look like. Here is the code:
radGridView1.MasterTemplate.AutoGenerateColumns = false;
radGridView1.MasterTemplate.Columns.Add(new GridViewTextBoxColumn("Name"));
radGridView1.MasterTemplate.Columns.Add(new GridViewTextBoxColumn("Attributes"));
radGridView1.MasterTemplate.Columns.Add(new GridViewTextBoxColumn("LastAccessTime"));
radGridView1.MasterTemplate.Columns.Add(new GridViewTextBoxColumn("CreationTime"));
DirectoryInfo directory = new DirectoryInfo("C:\\");
FileSystemInfo[] filesInDirectory = directory.GetFileSystemInfos();
radGridView1.DataSource = filesInDirectory;
As you can see, I have set not to autogenerate the columns. Then I define the columns I want. And then just bind the datasource. This will give you the order you want
Do let me know if this works for you.

I'm walking on VB.NET and I did something like this ..
dgv.Columns(Col4).DisplayIndex = 0
dgv.Columns(Col3).DisplayIndex = 1
dgv.Columns(Col2).DisplayIndex = 2
dgv.Columns(Col1).DisplayIndex = 3
in C# you can find it here

The grid will read and create its columns according to the fields in your object:
class MyObj
{
public string Column2 { get; set; }
public int Column1 { get; set; }
}
The above code will first add Column2 and then Column1 in your grid, while the following code will do the opposite:
class MyObj
{
public int Column1 { get; set; }
public string Column2 { get; set; }
}
Same goes for DataTables, it just reads the columns in the order they are.
To reorder them you can use the Move method as you already found out.

Related

c# How to call existing item of form having its name stored in new string?

I have multiple datagrids and i would like to move data from one to another.
The problem is:
I have their names "made" into string variables:
string gridfrom = "datagrid" + cplist1.SelectedItem.ToString();
string gridto = "datagrid" + cplist2.SelectedItem.ToString();
In this case, i know that gridfrom = datagridMAR and gridto = datagridAPR
But gridfrom and gridto changes according to the listboxes selected items.
How do i call these objects (already existing in the form) to change its properties? For example:
gridto.DataSource = gridfrom;
Thanks in advance
Let me tell you an elegant way of dealing with this situation without getting into dynamic type loading, etc
Create a custom type, say
class GridName {
public DataGridView Grid {get; private set;}
public string Name {get; private set;}
public GridName(DatagridView grid, string name) {
Grid = grid;
Name = name;
}
public string override ToString() {
return Name;
}
}
Now, instead of adding a string into the cplist1 and cplist2, simply add GridName object into it. It will display the name correctly and also when you want the grid associated with it, you can access the .Grid property
There are many other ways of doing it. For example, you can use ValueMember and DisplayMember property.
I would definitely resist against trying to convert the grid name into a DatagridView object.

Dynamic N columns bind to DetailsView?

I have a stored procedure that return the data below:
HEIGHT LENGTH WEIGHT WIDTH
0 0 0 0
The columns will be dynamic, can have up to N columns.
What I want to do is bind this result to Details View then let user key in the value.
But I have the error below when I bind to Details View:
DetailsView with id 'detailsViewProcessParameter' did not have any properties or attributes from which to generate fields. Ensure that your data source has content.
Here is the method that I use to return list of object then bind to details view:
public static List<object> GetProcessParameters(int formulaId)
{
using (var db = new SurfaceTreatment.SurfaceTreatmentEntities())
{
var paramFormulaId = new SqlParameter { ParameterName = "formulaId", Value = formulaId };
var query = db.Database.SqlQuery<object>("exec usp_GetProcessParameters #formulaId", paramFormulaId).ToList();
return query.ToList();
}
}
I suspect that I should use "select new { PropertyName = Value, PropertyName = Value}" in my LINQ in order to get rid of the error.
But how do I select new when PropertyName(column name) are dynamic?
How do I use reflection to solve this?
Or is there a better way to do this?
The problem here is that you are returning and thus binding a list of objects i.e. List<object> to DetailsView. I suspect that your object doesn't have any public property which DetailsView needs to Generate Rows and thus throws the error.
When using a list to bind to DetailsView ( or say a GridView ), the object in context, which is represented by some Class, MUST have public properties . For example suppose we bind a List of User List<User> to DetailsView, then here the User class MUST have public properties which will form the Rows for DetailsView ( or Columns in GridView).
public class User
{
public string Name { get; set; }
public int Age { get; set; }
public string Mail { get; set; }
}
With this basic concept, check to see what does your query variable looks like and try to map this query variable to a Class with public properties.
Since in your case Columns are dynamic in nature, You must be first able to determine the total number of columns with there values, construct a corresponding DataTable at runtime and bind that data table to Detailsview.
If you set AutoGenerateRows to true for the control, all the rows should generate automatically. I do not see any need to perform reflection.
Note that any property which is set for DataKeyNames will not be editable.

Bind winforms datagridview rowheader

In C# Winforms DataGridView I am binding a List of class using the datagriview's datasource property. Is there a way to bind one of the class property to the datagriview's rowheader?
I didn't want to iterate all the rows and add row header values one by one because I will have thousands of records so I wanted the rowheader to get the values from the binded class when I set the datasource.
For example I have this class and I want the rowheaders to show the Customer ID.
private class Customer
{
public string CustomerID { get; set; }
public string CustomerFirstName { get; set; }
}
I'm afraid you have to implement datagridview's OnPaint-Method, where you paint the customer id in a loop. Once, I wanted to numerate my entries and I didn't find any other solution.

Get index by value regarding combobox for win apps c#

we can easily get index of combobox using FindString method
int index = cboCountryTwoCode.FindString(localJob.DeliveryCountryTwoCode.Trim());
cboCountryTwoCode.SelectedIndex = index;
so i just need to know is there any way to get index of combobox just finding by value instead of finding by text. please let me know is there any similar. if anything not there then how to achieve my objective that get index of combobox just finding by value. thanks
this way i am populating my combo
cboCountryTwoCode.DataSource = Utility.LoadCountry();
cboCountryTwoCode.DisplayMember = "CData";
cboCountryTwoCode.ValueMember = "CValue";
LoadCountry() will return datatable . thanks
In WinForms, ComboBox doesn't have an explicit key/value list of items. It has an ObjectCollection for Items.
I think you could implement ToString() on your objects to display its proper descriptions.
Then you should use some LINQ to find the correct item. Something like this:
class MyType
{
public int Id { get; set; }
public string Description { get; set; }
public override string ToString()
{
return Description;
}
}
var selectedObject = cb.Items.Cast<MyType>().SingleOrDefault(i => i.Id.Equals(myId));

WPF DataGrid - row for new entry not visible

The problem is, that the blank row in the DataGrid isn't appearing, ergo user can not add data.
Here is the code:
System.Collections.ObjectModel.ObservableCollection<CoreVocabularyEntry> dataList = new System.Collections.ObjectModel.ObservableCollection<CoreVocabularyEntry>();
public VocabularyToolWindow()
{
InitializeComponent();
dataList.Add(new CoreVocabularyEntry { Foreign = "ja", Native = "ano" });
ListCollectionView view = new ListCollectionView(dataList);
WordsDataGrid.ItemsSource = dataList;
WordsDataGrid.CanUserAddRows = true;
MessageBox.Show(view.CanAddNew.ToString());
}
I can't figure out why view.CanAddNew equals false. This looks like a pretty standart scenario, so there's probably something obvions I'm missing. Can someone tell me what is wrong with the code ? CoreVocabularyEntry is just the following:
public struct CoreVocabularyEntry : IVocabularyEntry
{
#region IVocabularyEntry Members
public string Foreign
{
get;
set;
}
public string Native
{
get;
set;
}
#endregion
}
Thx, J.K.
This may be more simple than above. I had this problem when I did NOT have a default constructor and therefore the datagrid did not know how to add the new row. My only constructor involved a bunch of information to be supplied. Go to the object of that the List<> is made from and add a constructor like:
public MyClass(){}
Hope this helps!
Move WordsDataGrid.CanUserAddRows = true; above the statement where you set the DataGrid's ItemSource.
EDIT:
Just noticed you didn't implement IEditableObject. You'll need to do that for using the editing features of the DataGrid.

Categories