如何更改gridview中列的顺序?
问题描述:
我想改变gridview列的顺序(第8位是第一位,第一位是第二位,第二位是第三位......)。我正在使用文本文件中的数据填充我的GridView(其中有9列)。代码是在这里:如何更改gridview中列的顺序?
public DataTable ConvertToDataTable(string filePath, int numberOfColumns)
{
DataTable tbl = new DataTable();
for (int col = 0; col < numberOfColumns; col++)
tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString()));
string[] lines = File.ReadAllLines(filePath);
List<string> ekran = new List<string>();
foreach (string line in lines)
{
var cols = line.Split(',');
DataRow dr = tbl.NewRow();
for (int cIndex = 0; cIndex < 9; cIndex++)
{
if (cols[cIndex].Contains('_'))
{
cols[cIndex] = cols[cIndex].Substring(0, cols[cIndex].IndexOf('_'));
dr[cIndex] = cols[cIndex];
}
else
{
cols[cIndex] += '_';
cols[cIndex] = cols[cIndex].Substring(0, cols[cIndex].IndexOf('_'));
dr[cIndex] = cols[cIndex];
}
}
for (int cIndex = 0; cIndex < 9;cIndex++)
if (dr[cIndex].ToString() == promenljiva)
tbl.Rows.Add(dr);
}
this.GridView1.DataSource = tbl;
this.GridView1.DataBind();
return tbl;
}
之后,我有这样的:
ConvertToDataTable(filePath, 9);
这是GridView控件:
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
我可怎么办呢?
答
之前设置
tbl.Columns[8].SetOrdinal(0);
网格的DataSource这将改变DataTable.DataColumnCollection内的DataColumn的Ordinal财产的方式,电网认为,作为第一列,您的第9列。当然所有其他列移动到一个新的指数位置
答
为进一步阅读,这里详细讨论!
https://www.codeproject.com/Questions/711397/How-to-Change-order-of-columns-in-gridview
How to reorder columns in gridview dynamically
那么一些添加/删除列件与 “geekwithblogs” 网站 http://geekswithblogs.net/dotNETvinz/archive/2009/06/03/move--autogenerate-columns-at-leftmost-part-of-the-gridview.aspx