Click here to Skip to main content
15,887,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i am adding data from textboxes to grid view.
here is no problem in adding a row
problem is with deleting
i have to delete rows from grid view if a row is checked in the gridview
i dont want to make any changes in database

here is my sample code
<table><tr>
 <td><asp:Label ID="lblFmlDate" runat="server" Text="Date"></td>
                <td><asp:TextBox ID="txtFmlDate"  runat="server" Enabled ="False">
                    <asp:ImageButton ID="imgBtnFmlDate" ImageUrl="~/Images/Calendar_scheduleHS.png" runat="server" />
                    <AjaxToolkit:CalendarExtender ID="calExtrFmlDate"  
                        OnClientDateSelectionChanged ="checkDate"  runat="server" 
                        TargetControlID="txtFmlDate" PopupButtonID="imgBtnFmlDate" Enabled="True">
                    
            </td></tr>
            <tr>
            <td><asp:Label ID="lblDisease" runat="server" Text="Disease"></td>
            <td><asp:TextBox ID="txtDisease" Enabled ="False" runat="server" ></td>
            <td><asp:Button ID="cmdDiseaseAdd" Enabled ="False" runat="server" Text="Add" 
                    width="60px" onclick="cmdDiseaseAdd_Click" /></td>
            <td><asp:Button ID="cmdDiseaseDel" Enabled ="False" runat="server" Text="Delete" 
                    width="60px" onclick="cmdDiseaseDel_Click"/></td>
            </tr><tr>
            <td colspan="6">
            <asp:GridView ID="grdFmlHist" runat="server" AllowPaging="True" Enabled ="False" 
            AutoGenerateColumns="False" DataKeyNames="Relationship" Height="40px" WIDTH="450px"
            PageSize="8" ShowFooter="True" AutoGenerateDeleteButton="True" 
                    onrowdeleting="grdFmlHist_RowDeleting" >
            <pagersettings mode="NumericFirstLast" />
            <rowstyle backcolor="WhiteSmoke" bordercolor="CornflowerBlue" forecolor="Black">
                Height="30px" />
            <columns>
            <asp:TemplateField HeaderText="S.No">
                  <itemtemplate>
                      <asp:CheckBox ID="chkSelect" runat="server" />
                </itemtemplate>
                
                 <asp:TemplateField HeaderText="S.No">
                  <itemtemplate>
                    <%# ((GridViewRow)Container).RowIndex + 1%>
                </itemtemplate>
                
                <asp:BoundField DataField="Relationship" HeaderText="Relationship" />
                <asp:BoundField DataField="Disease" HeaderText="Disease" />
                <asp:BoundField DataField="Dates" HeaderText="Dates" />
              </columns>
            <footerstyle backcolor="Silver" height="25px" />
            <pagerstyle backcolor="DarkGray" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        
            </rowstyle></td></tr></table>


source code
 protected void cmdDiseaseAdd_Click(object sender, EventArgs e)
        {
            List<fmldiseasehist> fmlDiseaseList = new List<fmldiseasehist>();
            FmlDiseaseHist objFmlDiseaseHist = new FmlDiseaseHist();
            if (Session["FmlHist"] != null)
            {
                fmlDiseaseList = (List<fmldiseasehist>)(Session["FmlHist"]);
            }

            if (string.IsNullOrEmpty(txtRelation.Text) != true)
            {
                objFmlDiseaseHist.Relationship = Convert.ToString(txtRelation.Text);
            }
            if (string.IsNullOrEmpty(txtFmlDate.Text) != true)
            {
                objFmlDiseaseHist.Dates = txtFmlDate.Text;
            }
            if (string.IsNullOrEmpty(txtDisease.Text) != true)
            {
                objFmlDiseaseHist.Disease = txtDisease.Text;
            }
            txtRelation.Text = "";
            txtFmlDate.Text = dates.Date_Convert(dates.CurDateyyyymmdd());
            txtDisease.Text="";
            fmlDiseaseList.Add(objFmlDiseaseHist);
            Session["FmlHist"] = fmlDiseaseList;
            grdFmlHist.DataSource = fmlDiseaseList;
            grdFmlHist.DataBind();
           
         }
 protected void cmdDiseaseDel_Click(object sender, EventArgs e)
        {
            List<fmldiseasehist> fmlDiseaseList = new List<fmldiseasehist>();
            CheckBox chk = new CheckBox();
            if (chkDelMandatory ()== true)
            {
               foreach (GridViewRow gvr in grdFmlHist.Rows)
                        {
                            chk = (CheckBox)(gvr.Cells[0].FindControl("chkSelect"));
                            if (chk.Checked == true)
                            {
                                grdFmlHist.DeleteRow(gvr.RowIndex);
                            }
                        }
                           
            }
           
       }
protected void grdFmlHist_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }
Posted
Updated 2-Aug-11 21:11pm
v2
Comments
koolprasad2003 3-Aug-11 3:25am    
What problem occour when u click on delete, is it thows Index out of range ? Try databind() after deleting row

1 solution

I think you are getting index out of range.
In My opinion instead of deleting row from the grid view as it is binded to a data source try to delete the data from that Data source itself and rebind your grid view.

C#
protected void cmdDiseaseDel_Click(object sender, EventArgs e)
        {
            List<fmldiseasehist> fmlDiseaseList = new List<fmldiseasehist>();
            CheckBox chk = new CheckBox();
            if (chkDelMandatory ()== true)
            {
               foreach (GridViewRow gvr in grdFmlHist.Rows)
                        {
                            chk = (CheckBox)(gvr.Cells[0].FindControl("chkSelect"));
                            if (chk.Checked == true)
                            {
                   string item=grdFmlHist.Rows[e.RowIndex].Cells[yourDataCells].Text;
                                //delete item from fmlDiseaseList which is in                view state and rebind your grid
                            }
                        }
                           
            }
protected void grdFmlHist_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
          e.Cancle=true;
        }</fmldiseasehist></fmldiseasehist>


Hope this might help you.
 
Share this answer
 
v2

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