|
5.18 How do I move columns in a datagrid?
|
The columns appear in the order that their column styles were added to the tablestyle being used by the grid.
If you want to change this order, you would need to create a new table style, and add the columnstyles in the
order you want things to appear. Here is some code snippets that suggest how to do this.
|
public void MoveColumn(DataGrid _dataGrid, string _mappingName, int fromCol, int toCol)
|
if(fromCol == toCol) return;
|
DataGridTableStyle oldTS = _dataGrid.TableStyles[_mappingName];
|
DataGridTableStyle newTS = new DataGridTableStyle();
|
newTS.MappingName = _mappingName;
|
for(int i = 0; i < oldTS.GridColumnStyles.Count; ++i)
|
if(i != fromCol && fromCol < toCol)
|
newTS.GridColumnStyles.Add(oldTS.GridColumnStyles[i]);
|
newTS.GridColumnStyles.Add(oldTS.GridColumnStyles[fromCol]);
|
if(i != fromCol && fromCol > toCol)
|
newTS.GridColumnStyles.Add(oldTS.GridColumnStyles[i]);
|
_dataGrid.TableStyles.Remove(oldTS);
|
_dataGrid.TableStyles.Add(newTS);
|
private void button1_Click(object sender, System.EventArgs e)
|
MoveColumn(myDataGrid, "Customers", 3, 1);
|
Public Sub MoveColumn(_dataGrid As DataGrid, _mappingName As String, fromCol As Integer, toCol As Integer)
|
Dim oldTS As DataGridTableStyle = _dataGrid.TableStyles(_mappingName)
|
Dim newTS As New DataGridTableStyle()
|
newTS.MappingName = _mappingName
|
While i < oldTS.GridColumnStyles.Count
|
If i <> fromCol And fromCol < toCol Then
|
newTS.GridColumnStyles.Add(oldTS.GridColumnStyles(i))
|
newTS.GridColumnStyles.Add(oldTS.GridColumnStyles(fromCol))
|
If i <> fromCol And fromCol > toCol Then
|
newTS.GridColumnStyles.Add(oldTS.GridColumnStyles(i))
|
_dataGrid.TableStyles.Remove(oldTS)
|
_dataGrid.TableStyles.Add(newTS)
|
Private Sub button1_Click(sender As Object, e As System.EventArgs)
|
MoveColumn(myDataGrid, "Customers", 3, 1)
|
|
|
|