【问题标题】:delete selected rows from gridview on selecting option from a dropdownlist located outside the gridview从位于gridview外部的下拉列表中选择选项时从gridview中删除选定的行
【发布时间】:2013-04-12 11:15:30
【问题描述】:

我有一个下拉列表和一个 gridview 控件。 DropDownlist 在gridview 之外。 下拉列表包含一个选项删除。 Gridview 包含复选框,以便可以选择行。

这里是gridview的代码:

<asp:GridView ID="gvRefDetail" runat="server" CssClass="mGrid" AutoGenerateColumns="False"
                        PageSize="50" Font-Names="Segoe UI" Font-Size="10pt" Width="800" AllowPaging="true"
                        BackColor="White" BorderColor="Silver" EmptyDataText="No Record" BorderStyle="Double"
                        BorderWidth="1px" CellPadding="4">
                        <HeaderStyle BackColor="red" />
                        <RowStyle BackColor="White" ForeColor="#003399" />
                        <Columns>

                            <asp:TemplateField>
                                <HeaderTemplate>
                                    <input id="Checkbox2" type="checkbox" onclick="CheckAll(this)" runat="server" /></HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="NameofReferred">
                                <ItemStyle Wrap="true" HorizontalAlign="Left" />
                                <ItemTemplate>
                                    <asp:Label ID="lbltext" runat="server" Text='<%#Bind("NameofReferred") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txttext" runat="server" Text='<%#Bind("NameofReferred")%>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="CustomerName">
                                <ItemStyle Wrap="true" HorizontalAlign="Left" />
                                <ItemTemplate>
                                    <asp:Label ID="lbltext" runat="server" Text='<%#Bind("CustomerName") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:TextBox ID="txttext" runat="server" Text='<%#Bind("CustomerName")%>'></asp:TextBox>
                                </EditItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <PagerStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
                        <AlternatingRowStyle BackColor="#CCFFFF" />
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <HeaderStyle BackColor="White" Font-Bold="True" ForeColor="Gray" BorderColor="Gray" />
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <HeaderStyle Font-Bold="True" ForeColor="White" />
                    </asp:GridView>

c#代码:

protected void ddlaction_SelectedIndexChanged(object sender, EventArgs e)
{

    if (ddlaction.SelectedValue == "1")
    {

        GridViewRow gvrow = gvRefDetail.SelectedRow;
        int id = Convert.ToInt32(gvrow.Cells[1].Text);
        string delref = "delete from tbl_Refferal where ID='" + id + "'  ";
        con = new SqlConnection(conString);
        con.Open();
        SqlCommand cmd2 = new SqlCommand(delref, con);
        int temp1 = cmd2.ExecuteNonQuery();
        con.Close();

        BindRef();

    }

    }

现在,我的问题是,当我从 gridview 中选择一行或多行并从下拉列表中选择删除选项时,必须删除所选行。

我该怎么做?

【问题讨论】:

  • 到目前为止你尝试了什么?服务器端代码请让我们以更好的方式轻松回答n。好吧,您只需要获取复选框选定的值并在 dropdownlist_selectedindex 上更改,如果您的 delete 项目被选中,然后触发 delete 查询(对于选中的复选框值)
  • @Satindersingh-我已经添加了c#代码。请看一下。

标签: asp.net gridview


【解决方案1】:

ddlaction_SelectedIndexChangedstringVariableContainCheckboxvalues 上的代码是从 gridview 复选框中提取字符串变量,格式为 ('1,3,5,6')

if (ddlaction.SelectedValue == "1")
{
  string delref = "delete from tbl_Refferal where ID in (@checkboxSelectedValues)";
  con = new SqlConnection(conString);
  SqlCommand cmd2 = new SqlCommand(delref, con);
  cmd2.Parameters.AddWithValue("@checkboxSelectedValues", stringVariableContainCheckboxvalues);
  con.Open();
  int temp1 = cmd2.ExecuteNonQuery();
  con.Close();
  BindRef();
}

获取多个复选框值:

foreach (GridViewRow row in gvRefDetail.Rows)
{
    CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
    if (cb != null && cb.Checked)
    {
       // fetch values in string
    }
}

【讨论】:

    猜你喜欢
    • 2020-01-10
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2020-11-13
    • 2014-02-12
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    相关资源
    最近更新 更多