关于DataGridView设置DispalyIndex后显示顺序不正确问题
关于DataGridView设置DispalyIndex后显示顺序不正确问题
编辑器vs2005
测试程序:
winform程序界面上已经存在一个DataGridView控件dataGridView1
public Form1()
{
InitializeComponent();
dataGridView1.DataSource = CreateTestTable();
SetColumnsOrder(dataGridView1);
}
private void SetColumnsOrder(DataGridView dgv)
{
dataGridView1.Columns["AccountID"].DisplayIndex = 0;
dataGridView1.Columns["RelationItemTypeID"].DisplayIndex = 1;
dataGridView1.Columns["RelationItemID"].DisplayIndex = 2;
dataGridView1.Columns["OperationDate"].DisplayIndex = 3;
dataGridView1.Columns["CreateDate"].DisplayIndex = 4;
dataGridView1.Columns["OperationValue"].DisplayIndex = 5;
dataGridView1.Columns["Remark"].DisplayIndex = 6;
dataGridView1.Columns["LastChangeTime"].DisplayIndex = 7;
dataGridView1.Columns["ID"].DisplayIndex = 8;
dataGridView1.Columns["BusinessTableCode"].DisplayIndex = 9;
}
//这是测试
private DataTable CreateTestTable()
{
DataTable dt = new DataTable();
DataColumn colID = new DataColumn("ID");
DataColumn colOperationValue = new DataColumn("OperationValue");
DataColumn colOperationDate = new DataColumn("OperationDate");
DataColumn colCreateDate = new DataColumn("CreateDate");
DataColumn colLastChangeTime = new DataColumn("LastChangeTime");
DataColumn colRemark = new DataColumn("Remark");
DataColumn colBusinessTableCode = new DataColumn("BusinessTableCode");
DataColumn colRelationItemTypeID = new DataColumn("RelationItemTypeID");
DataColumn colRelationItemID = new DataColumn("RelationItemID");
DataColumn colAccountID = new DataColumn("AccountID");
dt.Columns.AddRange(new DataColumn[] { colID, colOperationValue, colOperationDate, colCreateDate, colLastChangeTime
, colRemark,colBusinessTableCode,colRelationItemTypeID,colRelationItemID,colAccountID});
return dt;
}
在构造函数Form1()中设置DataGridView的显示列顺序时,显示的顺序与DisplayIndex属性设置的不一致。
希望的顺序:
AccountID
RelationItemTypeID
RelationItemID
OperationDate
CreateDate
OperationValue
Remark
LastChangeTime
ID
BusinessTableCode
而实际显示的顺序为:
AccountID
ID
RelationItemTypeID
RelationItemID
OperationValue
OperationDate
CreateDate
LastChangeTime
Remark
BusinessTableCode
不信在你的机器上试试。
如果把
dataGridView1.DataSource = CreateTestTable();
SetColumnsOrder(dataGridView1);
写到Form_Load()中或按扭事件中显示的顺序正确。
测试程序:
winform程序界面上已经存在一个DataGridView控件dataGridView1
public Form1()
{
InitializeComponent();
dataGridView1.DataSource = CreateTestTable();
SetColumnsOrder(dataGridView1);
}
private void SetColumnsOrder(DataGridView dgv)
{
dataGridView1.Columns["AccountID"].DisplayIndex = 0;
dataGridView1.Columns["RelationItemTypeID"].DisplayIndex = 1;
dataGridView1.Columns["RelationItemID"].DisplayIndex = 2;
dataGridView1.Columns["OperationDate"].DisplayIndex = 3;
dataGridView1.Columns["CreateDate"].DisplayIndex = 4;
dataGridView1.Columns["OperationValue"].DisplayIndex = 5;
dataGridView1.Columns["Remark"].DisplayIndex = 6;
dataGridView1.Columns["LastChangeTime"].DisplayIndex = 7;
dataGridView1.Columns["ID"].DisplayIndex = 8;
dataGridView1.Columns["BusinessTableCode"].DisplayIndex = 9;
}
//这是测试
private DataTable CreateTestTable()
{
DataTable dt = new DataTable();
DataColumn colID = new DataColumn("ID");
DataColumn colOperationValue = new DataColumn("OperationValue");
DataColumn colOperationDate = new DataColumn("OperationDate");
DataColumn colCreateDate = new DataColumn("CreateDate");
DataColumn colLastChangeTime = new DataColumn("LastChangeTime");
DataColumn colRemark = new DataColumn("Remark");
DataColumn colBusinessTableCode = new DataColumn("BusinessTableCode");
DataColumn colRelationItemTypeID = new DataColumn("RelationItemTypeID");
DataColumn colRelationItemID = new DataColumn("RelationItemID");
DataColumn colAccountID = new DataColumn("AccountID");
dt.Columns.AddRange(new DataColumn[] { colID, colOperationValue, colOperationDate, colCreateDate, colLastChangeTime
, colRemark,colBusinessTableCode,colRelationItemTypeID,colRelationItemID,colAccountID});
return dt;
}
在构造函数Form1()中设置DataGridView的显示列顺序时,显示的顺序与DisplayIndex属性设置的不一致。
希望的顺序:
AccountID
RelationItemTypeID
RelationItemID
OperationDate
CreateDate
OperationValue
Remark
LastChangeTime
ID
BusinessTableCode
而实际显示的顺序为:
AccountID
ID
RelationItemTypeID
RelationItemID
OperationValue
OperationDate
CreateDate
LastChangeTime
Remark
BusinessTableCode
不信在你的机器上试试。
如果把
dataGridView1.DataSource = CreateTestTable();
SetColumnsOrder(dataGridView1);
写到Form_Load()中或按扭事件中显示的顺序正确。