Click here to Skip to main content
14,934,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a checkbox list and a gridview.
<asp:GridView ID="grdColumns" runat="server" AutoGenerateColumns="False" Width="800px"
                                                   ShowFooter="true" CssClass="gridtable1">

<asp:TemplateField HeaderText="Remove">
                                                               <asp:Button runat="server" ID="btnRemove" Text="Remove" OnClick="btnRemove_Click" />

I need to add and remove a dynamic row from gridview on if i checked/unchecked any item of checkbox list.i have code written inside btnremove_click to remove dynamic row.
When i click on remove button it works fine.

now i want to call this button click event on selected index changed of checkboxlist so that a so that i could delete a paricular row which i want to delete from multiple rows in gridview. But it gives error.How to achieve this task?Is there any other way to do so?

Help Me...
protected void btnRemove_Click(object sender, EventArgs e)
          GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
          int rindex = gvRow.RowIndex;

//remaining code goes here
Updated 3-Jul-14 23:09pm
s0muel 4-Jul-14 5:19am
Could you clarify question, please.
You have DataView control and checkboxlist control or you have checkbox control inside each grid row and want to select some rows by clicking on them and then remove them?

any way if you will use not grid control events to remove rows, you need to be sure that "sender" is a grid. May be better to use gridview variable to access rows and manipulate them.
pwavell 4-Jul-14 5:23am
i have a checkbox list control and a gridview on form.when i click on any item of chkboxlist a dynamic row with that items text value is added in gridview.i also have a textbox inside gridview to display that checkboxlist item value. now if i uncheck that particular item i want to delete that particular row added.
s0muel 4-Jul-14 5:31am
so if you add this method as event hander for checkbox events, in result sender object will be is checkbox not grid and casting what you use will not work. if you want use same handler you try to change such casting to using directly grid
pwavell 4-Jul-14 5:53am
how to type cast from checkboxlist to gridview?
s0muel 4-Jul-14 7:03am
what about use something like this?

List<int> lIds = new List<int>();
for (int i = 0; i < this.grid.Rows.Count; i++)
if ((this.grid.Rows[i].FindControl("CheckBoxValue") as System.Web.UI.WebControls.CheckBox).Checked)

1 solution

Simply you can add and remove directly from database and rebind the gridview.
You can use the event handler to call the button click event on the
 protected void Page_Load(object sender, EventArgs e)
 CheckboxID.Change += new EventHandler(this.btnRemove_Click );

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900