Column order on a DataGridView (Newbie)

W

Will

The code below defines a class FillData. This is used to bind to a
DataGridView. However, the columns on the DataGridView are NOT in the
same order as specified in the class (Start, Sweep, Duration, POW,
Sync) but instead are shown Start, Duration, Sync, Sweep, POW.

Is there any way I can ensure that the columns on the DataGridView are
displayed in a particular order?

Thanks

Will.

private FillData m_data;
grdFill.DataSource = m_data.FillRows;

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace MyApp.Objects
{
/// <summary>
/// SourceData breaks down the channel into fill data, tests and
other data
/// </summary>
public class FillData : EditableObject
{
public FillData(XmlNode node)
: base(node)
{
m_maxRows = int.Parse(node.Attributes["maxrows"].Value);

// Create some sample data
m_rows = new FillRow[m_maxRows];
for (int i = 0; i < m_rows.Length; ++i)
{
m_rows = new FillRow(i + 1);
m_rows.Start = rnd.Next(250);
m_rows.Sweep = rnd.Next(100);
m_rows.Duration = rnd.Next(200);
m_rows.POW = rnd.Next(250);
m_rows.Sync = rnd.Next(2) == 1;
}
}


public FillRow[] FillRows
{
get { return m_rows; }
}

public class FillRow
{
public FillRow(int i)
{
index = i;
}

public int Index
{
get { return index; }
}

public double Start
{
get { return start; }
set { start = value; }
}

public double Sweep
{
get { return sweep; }
set { sweep = value; }
}

public double Duration
{
get { return duration; }
set { duration = value; }
}

public double POW
{
get { return pow; }
set { pow = value; }
}

public bool Sync
{
get { return sync; }
set { sync = value; }
}

private int index;
private double start;
private double sweep;
private double duration;
private double pow;
private bool sync;
}

#region Private Data
FillRow[] m_rows;
int m_maxRows;
#endregion

// Random number generator
public static Random rnd = new Random();
}
}
 
C

ClayB

One way you can do this is to have your list implement ITypedList and
return the properties you want to see in the order you want to see
them in the ITypedList.GetItemProperties method.

private void Form1_Load(object sender, EventArgs e)
{
MyItems list = new MyItems();
list.Add( new MyItem("s0", "a0"));
list.Add( new MyItem("s1", "a1"));

this.dataGridView1.DataSource = list;
}

public class MyItems : List<MyItem> , ITypedList
{
#region ITypedList Members
public PropertyDescriptorCollection
GetItemProperties(PropertyDescriptor[] listAccessors)
{
PropertyDescriptorCollection pdc =
TypeDescriptor.GetProperties(typeof(MyItem));

return new PropertyDescriptorCollection(new
PropertyDescriptor[]
{
pdc["S"], pdc["A"]
});
}
public string GetListName(PropertyDescriptor[] listAccessors)
{
return "MyItem";
}
#endregion
}
public class MyItem
{
public MyItem(string s, string a)
{
this.s = s;
this.a = a;
}

string s;

public string S
{
get { return s; }
set { s = value; }
}

string a;

public string A
{
get { return a; }
set { a = value; }
}
}

================
Clay Burch
Syncfusion, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top