GridView删除行 [英] GridView Delete Row
本文介绍了GridView删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我所做的:
else if(e.CommandName ==DeletePart)
{
int index = Convert.ToInt32(e.CommandArgument); //#1 line
GridView1.DeleteRow(index); //#2 line
}
我收到的错误是:输入字符串没有正确的格式。 (这发生在#1行)...
这是gridview的样子:(使用链接按钮进行删除)
< asp:GridView ID =GridView1runat =serverAllowPaging =True
EmptyDataText =没有添加零件型号请添加一部分。
BorderColor =BlackBorderStyle =SolidBorderWidth =2px
CellPadding =3onrowcommand =GridView1_RowCommand
onrowediting =GridView1_RowEditingonrowdeleting =GridView1_RowDeleting>
<列>
< asp:TemplateField HeaderText =>
< ItemTemplate>
< asp:LinkButton ID =EditButtonrunat =serverCssClass =EditButtonCommandName =EditText =Edit/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =>
< ItemTemplate>
< asp:LinkButton ID =DeleteButtonrunat =serverCssClass =DeleteButtonCommandName =DeletePartCommandArgument ='<%#Container.DataItemIndex%>'Text =Delete/> ;
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>
< HeaderStyle BackColor =#FFFFCCBorderColor =BlackBorderStyle =Solid
BorderWidth =2px/>
< / asp:GridView>
Gridview如何填充和绑定的类:
public int companyID = 0;
public int methodID = 0;
DataTable dt;
#region SQLConnections
string connectionString = ConfigurationManager.ConnectionStrings [SOSConnectionString]。ConnectionString;
SqlConnection conn;
#endregion
protected void Page_Load(object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
dt = new DataTable();
MakeDataTable();
EmptyDataString();
}
else
{
dt =(DataTable)ViewState [DataTable];
}
ViewState [DataTable] = dt;
}
#region GridView
private void EmptyDataString()
{
TemplateBuilder tmpEmptyDataTemplate = new TemplateBuilder();
tmpEmptyDataTemplate.AppendLiteralString(没有零件已添加到模型中,请添加零件。
GridView1.EmptyDataTemplate = tmpEmptyDataTemplate;
GridView1.DataBind();
}
private void MakeDataTable()
{
dt.Columns.Add(Item);
dt.Columns.Add(Quantity);
dt.Columns.Add(价格P /数量);
}
private void AddToDataTable()
{
DataRow dr = dt.NewRow();
dr [Item] = txtPart.Text;
dr [Quantity] = numQuantity.Text;
dr [Price P / Quantity] = txtPricePQ.Text;
dt.Rows.Add(dr);
}
private void BindGrid()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btnAddPart_Click(object sender,EventArgs e)
{
AddToDataTable();
BindGrid();
ClearNewParts();
}
#endregion
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName ==Edit)
{
for(int i = 0; i< GridView1.Rows.Count; i ++)
{
GridViewRow row =(GridViewRow)(((LinkButton)e.CommandSource) .NamingContainer);
txtPart.Text = row.Cells [2] .Text;
numQuantity.Text = row.Cells [3] .Text;
txtPricePQ.Text = row.Cells [4] .Text;
}
}
else if(e.CommandName ==DeletePart)
{
// int iCount = GridView1.Rows.Count;
// for(int i = 1; i< = iCount; i ++)
// {
// GridView1.DeleteRow(i);
//}
// int rowIndex = Convert.ToInt32(GridView1.SelectedRow);
int index = Convert.ToInt32(e.CommandArgument);
GridView1.DeleteRow(index);
// int index = Convert.ToInt32(e.CommandArgument);
//GridView1.DeleteRow(rowIndex);
}
GridView1.DataBind();
}
解决方案
找到解决方案,只需要做一个数据库....这是工作代码:
else if(e.CommandName ==Delete)
{
int index = Convert.ToInt32(e.CommandArgument);
GridView1.DeleteRow(index);
((DataTable)ViewState [DataTable])。Rows [index] .Delete();
((DataTable)ViewState [DataTable])。AcceptChanges();
GridView1.DataSource =(DataTable)ViewState [Data];
GridView1.DataBind();
}
I'm trying to delete a row in a gridview (the data is not in the database yet), so it should be a simple delete row, if anybody can please point me in the right direction.... This is what I have done:
else if (e.CommandName == "DeletePart")
{
int index = Convert.ToInt32(e.CommandArgument); //#1 line
GridView1.DeleteRow(index); //#2 line
}
The error that I am receiving is : "Input string was not in a correct format." (this happens on #1 line)...
This is how the gridview looks like: (use a linkbutton to do the delete)
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
EmptyDataText="No parts has been added to the model, please add a part."
BorderColor="Black" BorderStyle="Solid" BorderWidth="2px"
CellPadding="3" onrowcommand="GridView1_RowCommand"
onrowediting="GridView1_RowEditing" onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="EditButton" runat="server" CssClass="EditButton" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="DeleteButton" runat="server" CssClass="DeleteButton" CommandName="DeletePart" CommandArgument='<%# Container.DataItemIndex %>' Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#FFFFCC" BorderColor="Black" BorderStyle="Solid"
BorderWidth="2px" />
</asp:GridView>
Class of how the gridview populated and binded:
public int companyID = 0;
public int methodID = 0;
DataTable dt;
#region SQLConnections
string connectionString = ConfigurationManager.ConnectionStrings["SOSConnectionString"].ConnectionString;
SqlConnection conn;
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dt = new DataTable();
MakeDataTable();
EmptyDataString();
}
else
{
dt = (DataTable)ViewState["DataTable"];
}
ViewState["DataTable"] = dt;
}
#region GridView
private void EmptyDataString()
{
TemplateBuilder tmpEmptyDataTemplate = new TemplateBuilder();
tmpEmptyDataTemplate.AppendLiteralString("No parts has been added to the model, please add a part.");
GridView1.EmptyDataTemplate = tmpEmptyDataTemplate;
GridView1.DataBind();
}
private void MakeDataTable()
{
dt.Columns.Add("Item");
dt.Columns.Add("Quantity");
dt.Columns.Add("Price P/Quantity");
}
private void AddToDataTable()
{
DataRow dr = dt.NewRow();
dr["Item"] = txtPart.Text;
dr["Quantity"] = numQuantity.Text;
dr["Price P/Quantity"] = txtPricePQ.Text;
dt.Rows.Add(dr);
}
private void BindGrid()
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btnAddPart_Click(object sender, EventArgs e)
{
AddToDataTable();
BindGrid();
ClearNewParts();
}
#endregion
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
txtPart.Text = row.Cells[2].Text;
numQuantity.Text = row.Cells[3].Text;
txtPricePQ.Text = row.Cells[4].Text;
}
}
else if (e.CommandName == "DeletePart")
{
//int iCount = GridView1.Rows.Count;
//for (int i = 1; i <= iCount; i++)
//{
// GridView1.DeleteRow(i);
//}
// int rowIndex = Convert.ToInt32(GridView1.SelectedRow);
int index = Convert.ToInt32(e.CommandArgument);
GridView1.DeleteRow(index);
//int index = Convert.ToInt32(e.CommandArgument);
//GridView1.DeleteRow(rowIndex);
}
GridView1.DataBind();
}
解决方案
Found the solution, only needed to do a databound.... Here is the working code:
else if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
GridView1.DeleteRow(index);
((DataTable)ViewState["DataTable"]).Rows[index].Delete();
((DataTable)ViewState["DataTable"]).AcceptChanges();
GridView1.DataSource = (DataTable)ViewState["Data"];
GridView1.DataBind();
}
这篇关于GridView删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文