我想改变gridview列的顺序(第8个是第一个,第一个是第二个,第二个是第三个......)。我正在使用来自文本文件的数据填充gridview(有9列)。代码在这里:
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>
我该怎么做?
答案 0 :(得分:3)
在设置网格的数据源之前
tbl.Columns[8].SetOrdinal(0);
这将以网格看到的方式更改DataTable.DataColumnCollection中DataColumn的Ordinal属性,作为第9列的第9列。当然,所有其他列都会移动到新的索引位置
答案 1 :(得分:0)
如需进一步阅读,请在此详细讨论!
https://www.codeproject.com/Questions/711397/How-to-Change-order-of-columns-in-gridview
How to reorder columns in gridview dynamically
然后一些用&#34; geekwithblogs&#34;添加/删除列片。现场 http://geekswithblogs.net/dotNETvinz/archive/2009/06/03/move--autogenerate-columns-at-leftmost-part-of-the-gridview.aspx