我有一個從數據庫中填充的gridview,我想允許用戶編輯並添加到gridview。完成更改/添加後,將其提交到數據庫。有人可以幫助我如何做到這一點?如何在寫入數據庫之前存儲可編輯gridview的數據
這也是迄今爲止
<asp:GridView ID="Grd" runat="server" AutoGenerateColumns="False" OnRowEditing="Grds_RowEditing"
OnRowCancelingEdit="Grd_RowCancelingEdit" OnRowUpdating="Grd_RowUpdating" Width="500px">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HiddenField ID="NameID" runat="server" Value='<%# Bind("ID")%>' />
<asp:Label ID="LblName" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<EditItemTemplate>
<asp:TextBox ID="TxtDate" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblDate" runat="server" Text='<%# Bind("Date")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView><asp:Button ID="btnok" Text="OK" Height="25" Width="25" runat="server" />
我的代碼和代碼背後
protected void Page_Load(object sender, EventArgs e)
{
BindGrid(int.Parse(hfID.Value));
}
protected void BindGrid(int iPerson)
{
DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
var qry = from p in dc.GetDetails(iPerson)
select p;
Grd.DataSource = qry;
Grd.DataBind();
}
protected void Grd_RowEditing(object sender, GridViewEditEventArgs e)
{
Grd.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
BindGrid(int.Parse(hfID.Value));
}
protected void Grd_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
Grd.EditIndex = -1;
BindGrid(int.Parse(hfID.Value));
}
protected void Grd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void btOK_Click(object sender, EventArgs e)
{
//update the database with changes
}
?或每行編輯 – sajanyamaha
是的,該按鈕不在gridview中。我剛剛更新了它的代碼 – user1801525