2013-08-22 44 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