radgrid中有多个删除行

时间:2012-09-17 09:42:12

标签: asp.net vb.net visual-studio telerik radgrid

enter image description here

我的问题:

我想使用RadGrid中的复选框删除所有选定的记录。 如何为此编写代码..

我有一个简单的GridView代码,但它在RadGrid中不起作用。

1 个答案:

答案 0 :(得分:1)

请尝试使用以下代码段。

方法1

的.aspx

 <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
        OnNeedDataSource="RadGrid1_NeedDataSource">
        <MasterTableView DataKeyNames="ID">
            <Columns>

                <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        CheckBox CheckBox1 = item.FindControl("CheckBox1") as CheckBox;
        if (CheckBox1 != null && CheckBox1.Checked)
        {
            // Access data key
            string strID = item.GetDataKeyValue("ID").ToString();
            // Access column
            string strName = item["Name"].Text; // "Name" is column unique name

            // delete logic comes here
        }
    }
}

方法2

的.aspx

  <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
        OnNeedDataSource="RadGrid1_NeedDataSource" 
        AllowMultiRowSelection="true">
        <MasterTableView DataKeyNames="ID">
            <Columns>
                <telerik:GridClientSelectColumn>
                </telerik:GridClientSelectColumn>
                <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                </telerik:GridBoundColumn>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <Selecting AllowRowSelect="true" />
        </ClientSettings>
    </telerik:RadGrid>

.aspx.cs

 protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.SelectedItems)
    {
        if (item.Selected)
        {
            // Access data key
            string strID = item.GetDataKeyValue("ID").ToString();
            // Access column
            string strName = item["Name"].Text; // "Name" is column unique name

            // delete logic comes here
        }
    }
}

上述两种方法的通用代码。

的.aspx

 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

.aspx.cs

 protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data = new[] {
        new { ID = 1, Name ="Name1",path="1.jpg"},
        new { ID = 2, Name = "Name2",path="2.jpg"},
        new { ID = 3, Name = "Name3",path="3.jpg"},
         new { ID = 4, Name = "Name4",path="2.jpg"},
        new { ID = 5, Name = "Name5",path="3.jpg"}
    };
    RadGrid1.DataSource = data;
}