Delete Row in GridView based on DropDownList Selection in ASP.Net

Last Reply on Nov 18, 2013 09:11 AM By Azim

Posted on Nov 18, 2013 08:35 AM

  My code as follows

 

  To what extent our institution is useful for you      Dropdownlist1(Good/Poor)

 

 When user select the Poor form the Dropdownlist1 in that case  ONLY POPUP screen will be displayed.

The POPUP Screen as follows

 

            Textbox(In that textbox user type the reason for Poor)

             Submit(Button)

 

When user Click the Submit (Button) the selected dropdownlist and Remarks will be displayed in the Gridview

 

 The Gridview as follows

              Dropdown           Reason

                 0                       The institution has to be improved for lot

 

instead of Select the Good from dropdownlist1, user wrongly select the Poor from the dropdownlist. that time POPUP screen is to displayed.

(Only when user select the Poor from the Dropdownlist POUP Screen is to be displayed in that textbox type the reason and click the Submit Button)

  Then Gridview as follows

              Dropdown           Reason

                 0                       The institution has to be improved for lot

 

Then user again select the Good from the Dopdownlist1 means Previously value in the Gridview

Dropdown and reason has to be deleted in the Gridview.

 

for that how can i do using csharp.

 

Regards,

Narasiman P.

 

 

 

 

 

   

You are viewing reply posted by: Azim on Nov 18, 2013 09:11 AM.
Posted on Nov 18, 2013 09:11 AM

This Way,

HTML:

<form id="form1" runat="server">
    <div>
        <cc1:ToolkitScriptManager runat="server">
        </cc1:ToolkitScriptManager>
        <asp:DropDownList ID="ddlMarks" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Marks_SelectedIndexChanged">
            <asp:ListItem Value="0" Text="Select" />
            <asp:ListItem Value="1" Text="Poor" />
            <asp:ListItem Value="2" Text="Good" />
        </asp:DropDownList>
        <br />
        <asp:GridView ID="gvReason" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="Marks" HeaderText="Marks" />
                <asp:BoundField DataField="Reason" HeaderText="Reason" />
            </Columns>
        </asp:GridView>
        <asp:Panel ID="pnlReason" runat="server">
            Reason
            <br />
            <asp:TextBox ID="txtReason" TextMode="MultiLine" Width="300" runat="server" /><br />
            <asp:Button ID="btnSave" runat="server" OnClick="SaveToGrid" Text="Save" />
            <asp:Button ID="btnCancel" Text="Cancel" runat="server" />
        </asp:Panel>
        <asp:LinkButton ID="lnkFake" Text="" runat="server" />
        <cc1:ModalPopupExtender ID="mpeReason" runat="server" PopupControlID="pnlReason"
            CancelControlID="btnCancel" BackgroundCssClass="modalpopup" TargetControlID="lnkFake"
            BehaviorID="mpeDemo">
        </cc1:ModalPopupExtender>
    </div>
    </form>

C#:

protected void Marks_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlMarks.SelectedItem.Value == "1")
        {
            this.mpeReason.Show();
        }
        if (ddlMarks.SelectedItem.Value == "2")
        {
            this.gvReason.DataSource = null;
            this.gvReason.DataBind();
        }
    }

    protected void SaveToGrid(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2]{new DataColumn("Marks",typeof(string)),
                                            new DataColumn("Reason",typeof(string))});
        string marks = ddlMarks.SelectedItem.Text;
        string reason = txtReason.Text.Trim() == string.Empty ? string.Empty : txtReason.Text.Trim();
        dt.Rows.Add(marks, reason);
        this.gvReason.DataSource = dt;
        this.gvReason.DataBind();
    }

STYLE:

<style type="text/css">
        .modalpopup
        {
            background-color: #ADADAD;
            filter: Alpha(Opacity=70);
            opacity: 0.70;
            -moz-opacity: 0.70;
        }
    </style>