有人可以解释一下我如何在gridview中动态编辑单元格。即只需点击它并输入,当我将焦点丢失到gridview时发布更改?
我已经尝试将单元格定义为模板控件(文本框)并让它显示并让我输入,但除此之外什么也没做。我甚至可以将基础数据集的默认值放入该单元格中。
ASP 2.0和VB.NET请。
答案 0 :(得分: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);
}
}
答案 1 :(得分:1)
使用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中的隐藏字段中,然后在返回时将新值与旧值进行比较,并仅在更改时更新数据库。
答案 2 :(得分: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