2013-08-22 122 views
2

我有一个GridView,其中一列是显示订单的字段,它们将显示在我的网站的前端。不必进入编辑页面中的每条记录,也不必通过这种方式更改顺序,只需单击一个按钮并使整个DisplayOrder(int)可编辑就会更方便,因此使生活变得更加简单。如何才能做到这一点?在ASP.net GridView中编辑一个列

回答

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

使用这一空白在任何情况下,我做到了在A按钮点击

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

     } 
+0

如何检索Save()方法的值? – RonaldPaguay

0

你可以试试这个使用的ShowDeleteButton =“真”在ASP CommandField中,你可以做的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> 

可能帮助你

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"可防止编辑特定字段(在行中)。

希望这会有所帮助。

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> 

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