【问题标题】:Directly editing a value in a gridview直接在网格视图中编辑值
【发布时间】:2012-05-02 06:54:53
【问题描述】:

谁能解释我如何简单地在网格视图中动态编辑单元格。即只需单击它并键入,然后在我失去对网格视图的关注时发布更改?

我尝试将单元格定义为模板控件(文本框)并让它显示并让我输入,但除此之外它什么也没做。我什至无法将底层数据集中的默认值放入该单元格中。

请使用 ASP 2.0 和 VB.NET。

【问题讨论】:

    标签: asp.net vb.net gridview


    【解决方案1】:
        <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>
    

    设置editable属性,然后使用grid的事件

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

    【讨论】:

      【解决方案2】:

      使用 TemplateField 而不是 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 的隐藏字段中,然后在返回时将新值与旧值进行比较,并且仅在更改时更新数据库。

      【讨论】:

      • 我知道的旧帖子,但这非常有用。只需要将 VB 转换为 C#。谢谢
      • 一个想法浮现在脑海中。你真的不应该在同一个html页面中有重复的id,有没有办法按类使用FindControl方法?
      • 它们并不是真正的重复 ID。 “真实” ID,即您查看源代码时看到的 ID,与您在 VB(或 C# 或其他)代码中看到的 ID 不同。 ASP.NET 会破坏 ID 以确保它是唯一的,这正是这种情况。
      【解决方案3】:

      只需将新的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
      

      【讨论】:

      • 我提到过 - 我想直接编辑单元格,而无需先单击编辑超链接。直接访问单元格以更新数据。
      • @AndrewMcLintock-更新答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 2016-10-21
      • 2013-05-23
      相关资源
      最近更新 更多