469,889 Members | 1,274 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

problem while removing row Dynamically from datagrid

Hello All,

I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically added row it also removes the row at the end along with the added row.

Plz tell me if, I am missing any thing.

Code[*]

</asp:datagrid>

Asp Code==>

<asp:datagrid id="PartGrid" Runat="server" AutoGenerateColumns="False" AllowPaging="True" ageSize="10">

<Columns>

<asp:TemplateColumn>

<ItemTemplate>

<asp:LinkButton id="btnDetail" Runat="server" CommandName="Expand">+</asp:LinkButton>

</ItemTemplate>

</asp:TemplateColumn>

<asp:BoundColumn DataField="partName" HeaderText="Part Name" />

<asp:BoundColumn DataField="partType" HeaderText="Part Type" />

<asp:BoundColumn DataField="status" HeaderText="Status" />

</Columns>

</asp:datagrid>

Code behind file (1) In the Item created event adding the Click event handler for the button

private void PartGrid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{

ListItemType eItemtype = e.Item.ItemType;

if ( eItemtype == ListItemType.AlternatingItem || eItemtype == ListItemType.Item)

{

btnDetail = e.Item.FindControl("btnDetail") as LinkButton;

btnDetail.Click += new EventHandler(btnDetail_Click);

}

}

(2)On click event Adding the new new row . In this event handler Adding the row if Button text is "+" else

I am trying to remove it. The Adding functionality working fine but while removing, it is also removing the extra row at the end. plz help me out.

private void btnDetail_Click(object sender, EventArgs e)

{

LinkButton btnTemp = sender as LinkButton;

if(btnTemp.Text == "+")

{

btnTemp.Text = "-";

DataGridItem item = (DataGridItem)((LinkButton)sender).NamingContainer ; //Getting the container row

int rowIndex = ((Table)(PartGrid.Controls[0])).Rows.GetRowIndex((TableRow)item);//its row index

DataGridItem dgRow = new DataGridItem(rowIndex + 1, 0, ListItemType.Item); //new Datagrid row
TableCell cell = new TableCell();

cell.ColumnSpan = item.Cells.Count;

cell.Controls.Add(new Label());

dgRow.Cells.Add(cell);

((Table)(PartGrid.Controls[0])).Rows.AddAt(rowIndex + 1, dgRow);

}

else

{

btnTemp.Text = "+";

DataGridItem item = (DataGridItem)((LinkButton)sender).NamingContainer ;

int rowIndex = ((Table)(PartGrid.Controls[0])).Rows.GetRowIndex((TableRow)item);

((Table)(PartGrid.Controls[0])).Rows.RemoveAt(rowIndex + 1);

}

}

Thanks in Advance

Sameer

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>VFXa6qEAtkSlvcAKrcX1OA==</Id>
Nov 17 '05 #1
0 2227

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Shravan Kumar | last post: by
2 posts views Thread by Dave Bailey | last post: by
4 posts views Thread by The Alchemist | last post: by
7 posts views Thread by Girish | last post: by
4 posts views Thread by Jeff User | last post: by
4 posts views Thread by sydney.luu | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.