PDA

View Full Version : نحوه جابجا کردن ستونها در Datagrid هنگام اجرای برنامه



Amin_tus
دوشنبه 12 بهمن 1383, 02:30 صبح
میخواهم ستونهای Datagrid را darg and drop کنم چیکار کنم؟

bbehnam
دوشنبه 12 بهمن 1383, 12:37 عصر
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 thiings to appear. Here is some code snippets that suggest how to do this.

[C#]

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

if(i == toCol)

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

}



//sample usage

private void button1_Click(object sender, System.EventArgs e)

{

MoveColumn(myDataGrid, "Customers", 3, 1);

}



[VB.NET]

Public Sub MoveColumn(_dataGrid As DataGrid, _mappingName As String, fromCol As Integer, toCol As Integer)

If fromCol = toCol Then

Return

End If

Dim oldTS As DataGridTableStyle = _dataGrid.TableStyles(_mappingName)

Dim newTS As New DataGridTableStyle()

newTS.MappingName = _mappingName



Dim i As Integer

i = 0

While i < oldTS.GridColumnStyles.Count

If i <> fromCol And fromCol < toCol Then

newTS.GridColumnStyles.Add(oldTS.GridColumnStyles( i))

End If

If i = toCol Then

newTS.GridColumnStyles.Add(oldTS.GridColumnStyles( fromCol))

End If

If i <> fromCol And fromCol > toCol Then

newTS.GridColumnStyles.Add(oldTS.GridColumnStyles( i))

End If

i = i + 1

End While

_dataGrid.TableStyles.Remove(oldTS)

_dataGrid.TableStyles.Add(newTS)

End Sub 'MoveColumn



'sample usage

Private Sub button1_Click(sender As Object, e As System.EventArgs)

MoveColumn(myDataGrid, "Customers", 3, 1)

End Sub 'button1_Click

Amin_tus
پنج شنبه 15 بهمن 1383, 02:49 صبح
سلام دوست عزیز من این کد را که از سایت
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp?print=764
در اینجا چسبانده ای قبلاً دیده بودم ولیکن این کد بخوبی جواب نمیدهد آیا خودت یکبار آن را امتحان کرده ای؟

bbehnam
شنبه 17 بهمن 1383, 15:39 عصر
سلام
بله من در جاهای مختلف از این کد استفاده کرده ام سعی میکنم یک نمونه از آن را در اینجا قرار دهم

Amin_tus
دوشنبه 19 بهمن 1383, 04:31 صبح
پس بنابراین از کمکت ممنونم
اگر میتوانی حتماً یک نمونه قرار بده تا من هم ببینم