使列可在ASP.net GridView中编辑

时间:2013-08-22 09:21:16

标签: c# asp.net gridview

我有一个GridView,其中一列是用于显示在我的网站前端的字段的显示顺序。而不是进入编辑页面中的每个记录并且必须以这种方式更改顺序,能够单击按钮并使整个DisplayOrder(int)可编辑更容易,因此使生活更容易。怎么办呢?

6 个答案:

答案 0 :(得分:7)

答案 1 :(得分:2)

试试这段代码:

 <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();

        }

答案 2 :(得分:0)

您可以在asp命令域中使用ShowDeleteButton =“true”来尝试使用gridview edidable: -

<Columns>
     <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
      <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
      <asp:BoundField DataField="Name" HeaderText="Name"    SortExpression="Name" />
   </Columns>

可能会帮到你

答案 3 :(得分:0)

您可以尝试以下内容:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton = "true">
       <Columns>
            <asp:BoundField DataField="prodId" HeaderText="Product Id" SortExpression="prodId" ReadOnly = "true" />
            <asp:BoundField DataField="prodQuantity" HeaderText="Quantity" 
                                SortExpression="prodQuantity" ReadOnly = "true" />
        </Columns>
</asp:GridView>

Gridview级设置AutoGenerateEditButton = "true"。这将使用户能够编辑行。 在数据字段级别使用ReadOnly = "true"来阻止编辑特定字段(在行中)。

希望这有帮助。

答案 4 :(得分:0)

你可以试试这个,它会在列中给出文本框     

    <asp:Label ID="DescriptionLabel" runat="server"
        Text='<%# Eval("Description") %>'></asp:Label>

    <asp:TextBox ID="Description" runat="server"
        Text='<%# Eval("Description") %>' Width="175px"
        visible="false"></asp:TextBox>

</ItemTemplate>

答案 5 :(得分:0)

link得到了我正在寻找的答案。如果您有自定义数据源,则必须处理编辑GridView引发的每个编辑事件。就我而言:

Protected Sub gMaterias_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs) Handles gMaterias.RowCancelingEdit
    gMaterias.EditIndex = -1
    BindData()
End Sub

Protected Sub gMaterias_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles gMaterias.RowEditing
    gMaterias.EditIndex = e.NewEditIndex
    BindData()
End Sub

Protected Sub gMaterias_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles gMaterias.RowUpdating
    lError.Visible = False
    lError.Text = ""
    Dim idMateria As Integer = e.Keys(0)
    Dim row As GridViewRow = gMaterias.Rows(e.RowIndex)
    Dim tbl As DataTable = Session("Materias")

    tbl.Rows(row.DataItemIndex)("universidad") = CType(gMaterias.Rows(e.RowIndex).Cells(5).Controls(0), TextBox).Text

    Dim calf = CType(gMaterias.Rows(e.RowIndex).Cells(6).Controls(0), TextBox).Text
    If IsNumeric(calf) Then
        tbl.Rows(row.DataItemIndex)("calificacion") = calf
    Else
        lError.Visible = True
        lError.Text = "La calificación no es válida"
    End If

    gMaterias.EditIndex = -1
    BindData()
End Sub