C# 如何在动态Gridview中删除行?
作为一个测试项目,我正在ASP.NET中为一个简单的库存系统工作。在一个页面中,我必须创建用于输入购买详细信息的页面!我使用动态gridview来简化数据输入。我使用了,但在删除gridview中的行时遇到问题。我已经看到了这一点,但它没有帮助 aspx代码如下所示-C# 如何在动态Gridview中删除行?,c#,asp.net,gridview,C#,Asp.net,Gridview,作为一个测试项目,我正在ASP.NET中为一个简单的库存系统工作。在一个页面中,我必须创建用于输入购买详细信息的页面!我使用动态gridview来简化数据输入。我使用了,但在删除gridview中的行时遇到问题。我已经看到了这一点,但它没有帮助 aspx代码如下所示- <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2> Purchase Managem
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Purchase Management</h2>
<asp:GridView ID="PurchaseMgmtGridView" runat="server" ShowFooter="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="PurchaseMgmtGridView_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Item">
<ItemTemplate>
<asp:DropDownList ID="ItemDropDownList" runat="server" AppendDataBoundItems="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ItemUnit">
<ItemTemplate>
<asp:DropDownList ID="ItemUnitDropDownList" runat="server" AppendDataBoundItems="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rate">
<ItemTemplate>
<asp:TextBox ID="RateTextBox" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty.">
<ItemTemplate>
<asp:TextBox ID="QtyTextBox" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="TotalLabel" runat="server">
</asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAddNewRow" runat="server" Text=" + " OnClick="ButtonAddNewRow_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</asp:Content>
知道我哪里错了吗
备注:代码更新我[提供了实体列表的代码,即Item和ItemUnit]我认为您试图访问指向null的对象引用 像这样试试
private void SetPreviousData()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count-1; i++)
{
DropDownList itemDropDownList =
(DropDownList)PurchaseMgmtGridView.Rows[i].Cells[0].FindControl("ItemDropDownList");
DropDownList itemUnitDropDownList =
(DropDownList)PurchaseMgmtGridView.Rows[i].Cells[1].FindControl("ItemUnitDropDownList");
TextBox rateTextBox =
(TextBox)PurchaseMgmtGridView.Rows[i].Cells[2].FindControl("RateTextBox");
TextBox qtyTextBox =
(TextBox)PurchaseMgmtGridView.Rows[i].Cells[3].FindControl("QtyTextBox");
Label totalLabel =
(Label)PurchaseMgmtGridView.Rows[i].Cells[4].FindControl("TotalLabel");
FillItemDropDownList(itemDropDownList);
FillItemUnitDropDownList(itemUnitDropDownList);
if (i < dt.Rows.Count - 1)
{
//itemDropDownList.SelectedValue = dt.Rows[i]["Item"].ToString();
//itemUnitDropDownList.SelectedValue = dt.Rows[i]["ItemUnit"].ToString();
itemDropDownList.ClearSelection();
itemDropDownList.Items.FindByText(dt.Rows[i]["Item"].ToString()).Selected = true;
itemUnitDropDownList.ClearSelection();
itemUnitDropDownList.Items.FindByText(dt.Rows[i]["ItemUnit"].ToString()).Selected = true;
}
rateTextBox.Text = dt.Rows[i]["Rate"].ToString();
qtyTextBox.Text = dt.Rows[i]["Qty"].ToString();
totalLabel.Text = dt.Rows[i]["Total"].ToString();
}
}
}
}
private void SetPreviousData()
{
如果(ViewState[“CurrentTable”]!=null)
{
DataTable dt=(DataTable)视图状态[“CurrentTable”];
如果(dt.Rows.Count>0)
{
对于(int i=0;i
请参阅此处,以了解问题的症结并不是删除GridView中的行,而是其他问题 首先,有两件事需要注意-我无法编译,因为我没有
Item
和ItemUnit
的定义,所以我通过阅读代码来完成。第二,我的咖啡还没喝完!(更新:我的咖啡喝完了!)
看起来SetPreviousData()
中对itemDropDownList
的引用是空的,所以请查看原因。使用foreach循环遍历该数据表的行可能更容易,以避免任何基于0的索引和计数1比较等问题(更新:这仍然更容易,但不会导致问题)
另外,不确定您是否打算这样做,但是获取ItemDropDownList的FindControl语句使用的是始终等于i
的rowIndex
。(更新:同样,这可能有助于清理代码,但这不是一项要求。)
首先,找出当它崩溃时,i
是什么,看看这是否是您所期望的,并找出FindControl语句无法正常工作的原因。如果是0,它可能试图读取标题行或不存在该下拉列表的内容
很抱歉,我不能给你一个明确的解决方案,但希望这能有所帮助
解决方案:
在获得完整的代码后,更容易看到发生了什么。基本上,PurchaseMgmtGridView\u rowdeleding
方法是从GridView中删除DropdownList,然后SetPreviousData()
尝试读取不存在的内容。SetPreviousData
中的FindControl
语句返回NULL,如错误消息中所示,但不是因为我先前推测的原因
从PurchaseMgmtGridView\u rowdeleding
方法中删除有问题的行,就可以全部设置好了
protected void PurchaseMgmtGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SetRowData();
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
int rowIndex = Convert.ToInt32(e.RowIndex);
if (dt.Rows.Count > 1)
{
dt.Rows.Remove(dt.Rows[rowIndex]);
drCurrentRow = dt.NewRow();
ViewState["CurrentTable"] = dt;
PurchaseMgmtGridView.DataSource = dt;
PurchaseMgmtGridView.DataBind();
// Delete this
//for (int i = 0; i < PurchaseMgmtGridView.Rows.Count - 1; i++)
//{
// PurchaseMgmtGridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
//}
SetPreviousData();
}
}
}
protectedvoid PurchaseMgmtGridView\u行删除(对象发送方,GridViewDeleteEventArgs e)
{
SetRowData();
如果(ViewState[“CurrentTable”]!=null)
{
DataTable dt=(DataTable)视图状态[“CurrentTable”];
DataRow drCurrentRow=null;
int rowIndex=Convert.ToInt32(e.rowIndex);
如果(dt.Rows.Count>1)
{
删除(dt.Rows[rowIndex]);
drCurrentRow=dt.NewRow();
视图状态[“当前表”]=dt;
PurchaseMgmtGridView.DataSource=dt;
PurchaseMgmtGridView.DataBind();
//删除这个
//对于(int i=0;i
如何获取object itemDropDownList?并将此方法发布到expection所在的位置异常是从SetPreviousData()方法引发的!特定代码为DropDownList itemDropDownList=(DropDownList)PurchaseMgmtGridView.Rows[rowIndex].Cells[0].FindC
private void SetPreviousData()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count-1; i++)
{
DropDownList itemDropDownList =
(DropDownList)PurchaseMgmtGridView.Rows[i].Cells[0].FindControl("ItemDropDownList");
DropDownList itemUnitDropDownList =
(DropDownList)PurchaseMgmtGridView.Rows[i].Cells[1].FindControl("ItemUnitDropDownList");
TextBox rateTextBox =
(TextBox)PurchaseMgmtGridView.Rows[i].Cells[2].FindControl("RateTextBox");
TextBox qtyTextBox =
(TextBox)PurchaseMgmtGridView.Rows[i].Cells[3].FindControl("QtyTextBox");
Label totalLabel =
(Label)PurchaseMgmtGridView.Rows[i].Cells[4].FindControl("TotalLabel");
FillItemDropDownList(itemDropDownList);
FillItemUnitDropDownList(itemUnitDropDownList);
if (i < dt.Rows.Count - 1)
{
//itemDropDownList.SelectedValue = dt.Rows[i]["Item"].ToString();
//itemUnitDropDownList.SelectedValue = dt.Rows[i]["ItemUnit"].ToString();
itemDropDownList.ClearSelection();
itemDropDownList.Items.FindByText(dt.Rows[i]["Item"].ToString()).Selected = true;
itemUnitDropDownList.ClearSelection();
itemUnitDropDownList.Items.FindByText(dt.Rows[i]["ItemUnit"].ToString()).Selected = true;
}
rateTextBox.Text = dt.Rows[i]["Rate"].ToString();
qtyTextBox.Text = dt.Rows[i]["Qty"].ToString();
totalLabel.Text = dt.Rows[i]["Total"].ToString();
}
}
}
}
protected void PurchaseMgmtGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SetRowData();
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
int rowIndex = Convert.ToInt32(e.RowIndex);
if (dt.Rows.Count > 1)
{
dt.Rows.Remove(dt.Rows[rowIndex]);
drCurrentRow = dt.NewRow();
ViewState["CurrentTable"] = dt;
PurchaseMgmtGridView.DataSource = dt;
PurchaseMgmtGridView.DataBind();
// Delete this
//for (int i = 0; i < PurchaseMgmtGridView.Rows.Count - 1; i++)
//{
// PurchaseMgmtGridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
//}
SetPreviousData();
}
}
}