如何在ASP.NET中使用LINQ to SQL更新和删除Gridview行 [英] How to Update and Delete the Rows of Gridview using LINQ to SQL in asp.net
问题描述
大家好,我尝试了不同的方法.我正在向Linq和Edm学习.我有一个带有编辑,更新删除事件的gridview.如果我尝试更新该行,则会出错.
Gridview的源代码:
Hi Guys , I tried in different ways. I am learning to Linq and Edm. I have a gridview with Edit, Update Deleting Events. If i am trying to update the row , i go the errors.
Source Code of Gridview :
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="false"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<AlternatingRowStyle BackColor="#F7F7F7" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lbnEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbnUpdate" runat="server" CausesValidation="True" CommandName="Update"
Text="Update" OnClientClick="return confirm('Update?')"></asp:LinkButton>
<asp:LinkButton ID="lbnCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lbnDelete" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete" OnClientClick="return confirm('Delete?')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SNO">
<ItemTemplate>
<asp:Label ID="lblSno" runat="server" Text='<%# Bind("SNO") %>'>'></asp:Label>
</ItemTemplate>
<%-- <EditItemTemplate>
<asp:TextBox ID="txtSno" runat="server" Text='<%# Bind("SNO") %>'>' ></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="BATCHNO">
<ItemTemplate>
<asp:Label ID="lblBatch" runat="server" Text='<%# Bind("BATCHNO") %>'>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBatch" runat="server" Text='<%# Bind("BATCHNO") %>'>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="JOBNO">
<ItemTemplate>
<asp:Label ID="lblJobNo" runat="server" Text='<%# Bind("JOBNO") %>'>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJobNo" runat="server" Text='<%# Bind("JOBNO") %>'>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LOTNO">
<ItemTemplate>
<asp:Label ID="lblLotNo" runat="server" Text='<%# Bind("LOTNO") %>'>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLotNo" runat="server" Text='<%# Bind("LOTNO") %>'>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MODIFIEDDATE">
<ItemTemplate>
<asp:Label ID="lblModifiedDate" runat="server" Text='<%# Bind("MODIFIEDDATE") %>'>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtModifiedDate" runat="server" Text='<%# Bind("MODIFIEDDATE") %>'>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
数据绑定到Gridview:
DataBinding to Gridview :
protected void Page_Load(object sender, EventArgs e)
{
binddata();
}
private void binddata()
{
LinqDataContext ld = new LinqDataContext();
var query = (from p in ld.BATCH_LOTs select new { p.SNO, p.BATCHNO, p.JOBNO, p.LOTNO, p.MODIFIEDDATE }).Distinct().Take(10);
GridView1.DataSource = query;
GridView1.DataBind();
}
更新行:
Updating the Row :
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
LinqDataContext ld = new LinqDataContext();
int Sno = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("lblSno") as Label).Text);
if (Sno == 0)
{
return;
}
var result = (from x in ld.BATCH_LOTs where x.SNO == Sno select x).FirstOrDefault();
result.BATCHNO = (GridView1.Rows[e.RowIndex].FindControl("txtBatch") as TextBox).Text;
result.JOBNO = (GridView1.Rows[e.RowIndex].FindControl("txtJobNo") as TextBox).Text;
result.LOTNO = (GridView1.Rows[e.RowIndex].FindControl("txtLotNo") as TextBox).Text;
result.MODIFIEDDATE = (GridView1.Rows[e.RowIndex].FindControl("txtModifiedDate") as TextBox).Text;
ld.SubmitChanges();
GridView1.EditIndex = -1;
binddata();
}
删除Gridview的行:
Deleting the Row of Gridview :
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
LinqDataContext ld = new LinqDataContext();
//Label sno = (Label)GridView1.Rows[e.RowIndex].FindControl("SNO");
//int x1 = Convert.ToInt32(sno);
string favoritesId = GridView1.Rows[e.RowIndex].Cells[0].Text;
var delQuery = from del in ld.BATCH_LOTs where del.SNO ==Convert.ToInt32( favoritesId) select del ;
foreach (var x in delQuery)
{
ld.BATCH_LOTs.DeleteOnSubmit(x);
}
try
{
ld.SubmitChanges();
}
catch
{
}
binddata();
}
我在更新和删除Gridview的行时遇到了错误.
I got the Errors in Updating and Deleting the Rows of Gridview.
推荐答案
hi
更新:
--------------
保留SNO的标签控件的ID为..."lblSno"
因此,从...更改代码.
hi
Update:
--------------
ID of label control that holds SNO is... "lblSno"
So change the code from...
int Sno = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("SNO") as Label).Text);
到
to
int Sno = Convert.ToInt32((GridView1.Rows[e.RowIndex].FindControl("lblSno") as Label).Text);
更好的代码将是..
better code will be..
int Sno = 0 ;
Label lblSno = GridView1.Rows[e.RowIndex].FindControl("lblSno") as Label
if (lblSno != null)
{
int.TryParse(lblSno.Text, out Sno);
}
这样可以防止错误.
对于删除
---------------------
我认为您的单元格索引(即
This way you can prevent error.
For Delete
---------------------
I think your cell index (i.e
Cells[0]
)不正确. plesae检查以下行
希望对您有帮助
) is not right. plesae check the following line
Hope this will help you
string favoritesId = GridView1.Rows[e.RowIndex].Cells[0].Text;
这篇关于如何在ASP.NET中使用LINQ to SQL更新和删除Gridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!