Problem while dynamically removing row from Data Grid

Discussion in 'ASP .Net Datagrid Control' started by Sameer Mowade, Apr 2, 2005.

  1. 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


    *** Sent via Developersdex http://www.developersdex.com ***
     
    Sameer Mowade, Apr 2, 2005
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. msnews.microsoft.com

    Legend for each row of the asp.net Data Grid Control.

    msnews.microsoft.com, Jun 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    924
    msnews.microsoft.com
    Jun 30, 2003
  2. Daniel Bass
    Replies:
    1
    Views:
    558
    Daniel Bass
    Jul 8, 2003
  3. Brian Henry

    data grid conditional row formatting

    Brian Henry, Jul 8, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,221
    Brian Henry
    Jul 8, 2004
  4. Joe Au
    Replies:
    2
    Views:
    4,320
    Joe Au
    Aug 2, 2004
  5. Bazza Formez
    Replies:
    4
    Views:
    587
    Vanni Tomezzoli
    Dec 9, 2004
  6. IK

    How to find grid's row that matches row in dataset

    IK, Sep 15, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    300
  7. D
    Replies:
    0
    Views:
    511
  8. Cylix
    Replies:
    0
    Views:
    252
    Cylix
    Jun 13, 2006
Loading...