c# - 使 ASP.net GridView 中的列可编辑

标签 c# asp.net gridview

我有一个 GridView,其中一列用于字段的显示顺序,这些字段将显示在我的网站的前端。与其进入编辑页面中的每条记录并必须以这种方式更改顺序,不如单击一个按钮并使整个 DisplayOrder (int) 可编辑,从而使生活变得更加轻松。如何做到这一点?

最佳答案

试试这个代码:

 <asp:ListBox ID="ListBox1" runat="server">
                <asp:ListItem>Manager1</asp:ListItem>
                <asp:ListItem>Manager2</asp:ListItem>
                <asp:ListItem>Manager3</asp:ListItem>
                <asp:ListItem>Manager4</asp:ListItem>
            </asp:ListBox>
            <asp:GridView ID="UserAllocationGrid" runat="server"
                AutoGenerateColumns="False">
            <Columns>

                <asp:BoundField DataField="Manager" HeaderText="Manager"
                    SortExpression="managers" />
                    <asp:TemplateField HeaderText="Allocation Percentage">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"
                            Text= '<%# Bind("AllocationPercentage") %>' BorderStyle="None"></asp:TextBox>
                    </ItemTemplate>
                    </asp:TemplateField>

            </Columns>
            </asp:GridView>

隐藏代码

     void fillGV()
        {
            DataTable UserAllocationTable = new DataTable();

            UserAllocationTable.Columns.Add("Manager");
            UserAllocationTable.Columns.Add("AllocationPercentage");
            // go through listbox1 to find selected managers = selectedManagersList 
            List<string> selectedManagersListDates = new List<string>();
            int counterR = 0;
            foreach (ListItem strItem in ListBox1.Items)
            {                     
                    //selectedManagersListDates.Add(strItem.Value); 
                    DataRow drManagerName = UserAllocationTable.NewRow();
                    UserAllocationTable.Rows.Add(drManagerName);
                    UserAllocationTable.Rows[counterR]["Manager"] = strItem.Value;
                    counterR = counterR + 1;            
            }
           // ViewState["UserAllocationTable"] = UserAllocationTable;
            UserAllocationGrid.DataSource = UserAllocationTable;
            UserAllocationGrid.DataBind(); 
        }

在任何情况下都使用这个void,我是通过单击按钮完成的

      protected void Button1_Click(object sender, EventArgs e)
        {
            fillGV();

        }

关于c# - 使 ASP.net GridView 中的列可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18376378/

相关文章:

c# - 调用方法并绘制

c# - 使用表达式树构造具有未知成员的对象

asp.net - 如何在 GridView 绑定(bind)字段中放置条件?

android - 获取图库中的所有图像

asp.net - ASP.NET 4.0 中不同的服务器和客户端缓存策略

c# - GridView 中的验证不适用于 EditItemTemplate 中的 TextBox

c# - 如何在 C# 中创建包含不同类型的字典

c# - 使用 Roslyn 替换 C# 中的标识符

asp.net - IHttpModule.BeginRequest 触发 2X,Application_BeginRequest 触发 1X

asp.net - 清除缓存后,set-cookie 不适用于 IE11/10