【问题标题】:How to Change order of columns in gridview?如何更改gridview中列的顺序?
【发布时间】:2017-03-31 15:13:51
【问题描述】:

我想更改 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);

这是网格视图:

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

我该怎么做?

【问题讨论】:

    标签: c# asp.net gridview position


    【解决方案1】:

    设置网格的DataSource之前

    tbl.Columns[8].SetOrdinal(0);
    

    这将更改 DataTable.DataColumnCollection 中 DataColumn 的 Ordinal 属性,网格将第一列视为您的第 9 列。当然,所有其他列都会向上移动到新的索引位置

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2021-01-26
      相关资源
      最近更新 更多