2012-05-02 241 views
1

有人可以解释一下如何在GridView中简单地动态编辑一个单元格。即只要点击它并键入,并在我失去焦点时发布更改到gridview?直接在gridview中编辑一个值

我试着定义一个单元格作为模板控件(textbox)并让它显示并让我输入,但除此之外它什么也没做。我甚至可以从底层数据集中获取默认值到该单元格中。

ASP 2.0和VB.NET请。

回答

0

只需添加一个新GridView页面,并从它的Smart Tag,点击Enable Editing

有关如何做到这一点,你可以看看从这里了解更多信息

http://msdn.microsoft.com/en-us/library/ms972948.aspx

你需要将您自己的控件添加到模板字段而不是绑定字段中,在gridvew中捕获此事件并相应地更新您的行。

的东西,你可以这样开始:

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) 

    Dim btn As Button = TryCast(e.CommandSource, Button) 
    Dim row As GridViewRow = TryCast(btn.NamingContainer, GridViewRow) 

    If row IsNot Nothing Then 
     Dim txt As TextBox = TryCast(row.FindControl("TextBox1"), TextBox) 
     If txt IsNot Nothing Then 
      ' Do something here 
     End If 
    End If 
End Sub 
+0

的事件我mentoned - 我想直接编辑单元格没有首先点击的编辑超链接。直接访问单元以更新数据。 –

+0

@ AndrewMcLintock - 更新了答案。 – coder

1

使用模板列,而不是一个BoundField。在TemplateField中放置一个TextBox。 (或一个复选框或任何你需要的。)把一个“更新”按钮放在屏幕上的某个地方。例如:

<asp:GridView ID="mydata" runat="server" DataSourceID="dsMydata" AutoGenerateColumns="false" > 
<Columns> 
<asp:BoundField DataField="Noneditablefield" HeaderText="Non-editable field" /> 
<asp:TemplateField HeaderText="Editable Field"> 
    <ItemTemplate> 
    <asp:TextBox ID="myfield" runat="server" Columns="10" text='<%# eval("myfield") %>' /> 
    </ItemTemplate> 
</asp:TemplateField> 
</asp:GridView> 

然后创建一个函数来处理更新按钮单击。在这个函数中,循环遍历表的各行,使用FindControl来获取字段,并执行您需要的任何操作。

Protected Sub update_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles update.Click 
    For Each row As GridViewRow In tasks.Rows 
    Dim MyField = CType(row.FindControl("myfield"), TextBox).Text 
    ' Do whatever you need with the new value 
    End For 
End Sub 

假设有多行,有些行会更新,有些不会。根据应用程序的性质,您可能只更新所有行,更改或不更改。当我感到懒惰时,我已经这样做了,而且我知道只有几条记录,并且没有两个用户在查看相同数据并同时进行更改的大问题。不过,更有可能的是,您希望将旧值保存在gridview中的隐藏字段中,然后在返回时将新值与旧值进行比较,并且只更新db。

+0

旧帖子我知道,但这是hella有用。只需将VB转换为C#。谢谢 – Halter

+0

然而,想到一个想法。你真的不应该在同一个html页面中重复'id',是否有一种方法可以通过class来使用'FindControl'方法呢? – Halter

+1

他们不是真正重复的ID。如果您查看源代码,“真实”ID(即您看到的ID)与您在VB(或C#或其他代码)中看到的ID不同。 ASP.NET修改了ID以确保它是唯一的,完全适用于这种情况。 – Jay

2
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" 
     ShowFooter="true" AllowPaging="true" PageSize="4" AllowSorting="True" OnRowCommand="GridView1_RowCommand" 
     OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" 
     OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSorting="GridView1_Sorting" 
     OnRowCancelingEdit="GridView1_RowCancelingEdit"> 
     <Columns> 
      <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
      <asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id"> 
       <EditItemTemplate> 
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Id") %>'></asp:Label> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="Id" ShowHeader="True" /> 
      <asp:TemplateField HeaderText="Name" SortExpression="Name"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label> 
       </ItemTemplate> 
       <FooterTemplate> 
        <asp:TextBox ID="QuantityTextBox" runat="server"></asp:TextBox> 
       </FooterTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Description" SortExpression="Description"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Description") %>'></asp:Label> 
       </ItemTemplate> 
       <FooterTemplate> 
        <asp:TextBox ID="DescriptionTextBox" runat="server"></asp:TextBox> 
       </FooterTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField> 
       <FooterTemplate> 
        <asp:LinkButton ID="btnNew" runat="server" CommandName="New" Text="New" /> 
       </FooterTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

设置可编辑的属性,然后使用网格

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     String StudentId = GridView1.Rows[e.RowIndex].Cells[1].Text; 
     String Firstname = GridView1.Rows[e.RowIndex].Cells[2].Text; 
     String Lastname = GridView1.Rows[e.RowIndex].Cells[3].Text; 
     String Email = GridView1.Rows[e.RowIndex].Cells[4].Text; 
     int id = Convert.ToInt32(StudentId); 
     Response.Write(StudentId); 
     try 
     { 
      studentEntities context = new studentEntities(); 
      tbl_Students dbstudent = context.tbl_Students.First(i => i.Studentid == id); 
      dbstudent.Firstname = Firstname; 
      dbstudent.Lastname = Lastname; 
      dbstudent.Email = Email; 
      context.SaveChanges(); 

     } 
     catch (Exception e1) 
     { 
      Console.WriteLine(e1.InnerException); 
     } 

    }