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

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


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...
C#
protected void btnRemove_Click(object sender, EventArgs e)
      {
          GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
          int rindex = gvRow.RowIndex;

//remaining code goes here
}
Posted
Updated 3-Jul-14 23:09pm
v3
Comments
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)
{
lIds.Add(i);
}
}

1 solution

Simply you can add and remove directly from database and rebind the gridview.
Or
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 );
}
 
Share this answer
 

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