从gridview外部的下拉列表中选择选项时,从gridview中删除选定的行

时间:2013-04-19 06:02:37

标签: asp.net gridview

我有一个下拉列表和一个gridview控件。 DropDownlist在gridview之外。 Dropdownlist包含一个选项Delete。 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#c​​ode:

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中选择一行或多行并从下拉列表中选择删除选项时,必须删除所选行。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

ddlaction_SelectedIndexChangedstringVariableContainCheckboxvalues上的代码是格式为('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
    }
}