Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改gridview中boundfield的顺序_C#_Asp.net_Gridview - Fatal编程技术网

C# 更改gridview中boundfield的顺序

C# 更改gridview中boundfield的顺序,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试开发一个应用程序,数据显示在Gridview中。gridview包含许多边界字段列。我最近在gridview的最后一个boundfield列(导入的boundfield列)末尾添加了一列。因此,我很少有机会将最后一个边界字段移动到标记中所需的位置。如果我将列移动到所需的位置,那么我必须在行数据绑定时修改整个编码。那么,我们有没有办法在不改变加价的情况下对列进行重新排序 <asp:BoundField DataField="someData" HeaderText="SomeDat

我正在尝试开发一个应用程序,数据显示在Gridview中。gridview包含许多边界字段列。我最近在gridview的最后一个boundfield列(导入的boundfield列)末尾添加了一列。因此,我很少有机会将最后一个边界字段移动到标记中所需的位置。如果我将列移动到所需的位置,那么我必须在行数据绑定时修改整个编码。那么,我们有没有办法在不改变加价的情况下对列进行重新排序

<asp:BoundField DataField="someData" HeaderText="SomeData"> </asp:BoundField>
 <asp:CommandField UpdateText="Update" EditText="Edit" CancelText="|Cancel" ShowEditButton="true" ControlStyle-CssClass="LinkNormal" />
 <asp:BoundField DataField="someData2" HeaderText="Imported"> </asp:BoundField> 

输出如下(编辑/删除/导入为边界字段列)

导入某些数据|更新|

我现在需要的gridview应该显示如下

导入|某些数据|更新


GridView似乎没有这样的功能。您可以尝试通过例如列的标题文本来获取代码隐藏中列的索引,而不是,下面是执行此任务的函数:

int getColumnIndexByHeaderText(GridView gridView, string headerText)
{
    for (int i = 0; i < gridView.Columns.Count; ++i)
    {
        if (gridView.Columns[i].HeaderText == headerText)
            return i;
    }
    return -1;
}
int getColumnIndexByHeaderText(GridView GridView,string headerText)
{
对于(int i=0;i

使用它而不是硬编码的列索引。

我建议不要使用列索引,开始使用数据键引用代码中的列值。这样,您就可以更改标记并在不影响代码的情况下移动列

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Col1, Col2, Col3" ... >

使用数据键可以完全解决对列索引的需要。@James,这要看情况而定。若索引只用于检索数据,那个么数据键将是一个足够且优雅的解决方案。但是,如果使用索引来设置列可见性,那么使用数据键就没有多大帮助。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    string col1 = GridView1.DataKeys[e.Row.RowIndex]["Col1"].ToString();
}