有人可以解釋一下如何在GridView中簡單地動態編輯一個單元格。即只要點擊它並鍵入,並在我失去焦點時發佈更改到gridview?直接在gridview中編輯一個值
我試着定義一個單元格作爲模板控件(textbox)並讓它顯示並讓我輸入,但除此之外它什麼也沒做。我甚至可以從底層數據集中獲取默認值到該單元格中。
ASP 2.0和VB.NET請。
有人可以解釋一下如何在GridView中簡單地動態編輯一個單元格。即只要點擊它並鍵入,並在我失去焦點時發佈更改到gridview?直接在gridview中編輯一個值
我試着定義一個單元格作爲模板控件(textbox)並讓它顯示並讓我輸入,但除此之外它什麼也沒做。我甚至可以從底層數據集中獲取默認值到該單元格中。
ASP 2.0和VB.NET請。
只需添加一個新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
使用模板列,而不是一個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。
<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);
}
}
的事件我mentoned - 我想直接編輯單元格沒有首先點擊的編輯超鏈接。直接訪問單元以更新數據。 –
@ AndrewMcLintock - 更新了答案。 – coder