Telerik Radgrid行删除并恢复动态添加新行的数据

时间:2016-09-12 14:05:41

标签: asp.net telerik telerik-grid

我使用以下代码只是为了显示带有“添加新课程”按钮的radgrid标题。最初在页面加载时我没有数据绑定到radgrid。在“添加新课程”按钮上单击我向RadGrid添加一个新行,其中包含2个文本框和2个下拉列表(从db绑定)。每行都应该有“删除行”按钮。我这些工作。现在我的问题是我需要恢复用户在尝试向网格添加另一个新行时输入的数据。还有怎么做我删除了一行?请通过您的建议帮助我,或者高亮显示我现有代码中的错误。提前感谢。

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" MasterTableView-CommandItemSettings-ShowAddNewRecordButton="false"  MasterTableView-CommandItemSettings-ShowRefreshButton="false" OnNeedDataSource="RadGrid1_NeedDataSource"
        OnItemDataBound="RadGrid1_ItemDataBound" OnItemCommand="RadGrid1_ItemCommand">
        <MasterTableView Width="100%" HeaderStyle-Font-Bold="true" CommandItemStyle-Font-Bold="true" DataKeyNames="IsAdd,BusID" CommandItemDisplay="Top" CommandItemStyle-HorizontalAlign="Right">
            <CommandItemTemplate>
                <asp:Button ID="IsAdd" Font-Size="Small" Font-Bold="true" CommandName="InitInsert" Text ="Add New Bus" runat="server" />
            </CommandItemTemplate>
            <Columns>
                <telerik:GridTemplateColumn UniqueName="BusID" HeaderText="Bus #" DataField="BusID">
                    <ItemTemplate>
                       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn UniqueName="Name"  HeaderText="Bus Series" DataField="Name">
                    <ItemTemplate>
                      <asp:DropDownList ID="SeriesDropDown" DataTextField="SeriesName" runat="server" AutoPostBack="false"></asp:DropDownList>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn UniqueName="Name" HeaderText="Bus Group" DataField="Name">
                    <ItemTemplate>
                       <asp:DropDownList ID="GroupDropDown" DataTextField="GroupName" runat="server" AutoPostBack="false"></asp:DropDownList>               
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn UniqueName="Name" HeaderText="VIN" DataField="Name">
                    <ItemTemplate>
                     <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                        <asp:Button ID="Button1" runat="server" Text="Remove Row" CommandName="Delete" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>

            </Columns>              

        </MasterTableView>         
    </telerik:RadGrid>  
    <asp:Button ID="savebtn" runat="server" Font-Bold="true" Text="Save Rows"/>

aspx.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

        if (!IsPostBack)
        {
            dt.Columns.Add("BusID");
            dt.Columns.Add("Name");
            dt.Columns.Add("IsAdd");

            dt.Rows.Add(1, "Name1", false);
            dt.Rows.Add(2, "Name2", false);
            dt.Rows.Add(3, "Name3", false);

            Session["dt"] = dt;
        }
    }

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = (DataTable)Session["dt"];
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        Button btn = new Button();           

        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
            Button Button1 = item.FindControl("Button1") as Button;                

            DropDownList SeriesDropDown = item.FindControl("SeriesDropDown") as DropDownList;
            DropDownList GroupDropDown = item.FindControl("GroupDropDown") as DropDownList;
            TextBox TextBox4 = item.FindControl("TextBox4") as TextBox;

            DataSet busGroup_Dataset = admin.GetBusGroup();
            GroupDropDown.DataSource = busGroup_Dataset;
            GroupDropDown.DataBind();

            DataSet busSeries_Dataset = admin.GetBusSeries();
            SeriesDropDown.DataSource = busSeries_Dataset;
            SeriesDropDown.DataBind();


            bool isAdd = Convert.ToBoolean(item.GetDataKeyValue("IsAdd"));
            if (isAdd)
            {
                TextBox1.Visible = SeriesDropDown.Visible = GroupDropDown.Visible = TextBox4.Visible = true;
                btn.Visible = true;
                RadGrid1.DataSource = Session["dt"];
            }
            else
            {
                TextBox1.Visible = SeriesDropDown.Visible = GroupDropDown.Visible = TextBox4.Visible = false;
                Button1.Visible = false;                   
            }
        }

    }


    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName)
        {
            DataTable dt = (DataTable)Session["dt"];
            dt.Rows.Add(0, string.Empty, true);
            RadGrid1.MasterTableView.IsItemInserted = false;
            e.Canceled = true;
            RadGrid1.Rebind();
        }
        if (e.CommandName == "Delete")
        {

            string ID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["0"].ToString();
            DataTable table = (DataTable)Session["dt"];
            if (table.Rows.Find(ID) != null)
            {
                table.Rows.Find(ID).Delete();
                table.AcceptChanges();
                Session["dt"] = table;
            }


            RadGrid1.Rebind();
        }
    }

1 个答案:

答案 0 :(得分:0)



  

Telerik RadGrid提供丰富的服务器API,用于插入新数据,   更新现有数据和删除数据。


正如telerik管理员在官方论坛上告诉你的那样,在8天前我告诉你这样做了。

请参阅演示中的魔术,在doc:

中了解更多相关信息


让我为你做!

  

以下示例在新/干净的telerik项目中开发,您只需复制它们即可尝试。

1 /。使用高级绑定Aka onNeedDataSource

在ASPX中:

    <telerik:RadGrid ID="RadGrid1"runat="server"
                     OnNeedDataSource="RadGrid1_NeedDataSource" 
                     GroupingEnabled="False" 
                     AutoGenerateColumns="False" 
                     OnInsertCommand="RadGrid1_InsertCommand" 
                     AllowSorting="True" 
                     AllowPaging="True">
    <MasterTableView CommandItemDisplay="Top" EditMode="Batch" >
        <CommandItemSettings ShowExportToPdfButton="false" ShowExportToExcelButton="false"  ShowPrintButton="false" />
        <Columns>
            <telerik:GridBoundColumn UniqueName="CourseID" HeaderText="CourseID" DataField="CourseID" DataType="System.Int32" >
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="CourseNumber" HeaderText="CourseNumber" DataField="CourseNumber" DataType="System.Int32">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Location" HeaderText="Location" DataField="Location" DataType="System.String" >
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="Name" HeaderText="Name" DataField="Name" DataType="System.String" >
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
    </telerik:RadGrid>

在CodeBehind中:

    public ObjectContainer myDatasource
{
    get
    {
        if (Session["test_01"] == null)
            Session["test_01"] = new ObjectContainer();

        return Session["test_01"] as ObjectContainer ;
    }
    set { Session["test_01"] = value; }
}

protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    YoData row = new YoData();
    ObjectContainer tempo = new ObjectContainer();
    GridBatchEditingEventArgument argument = e.CommandArgument as GridBatchEditingEventArgument;
    Hashtable newValues = argument.NewValues;

    row.CourseID = Int32.Parse(newValues["CourseID"].ToString());
    row.CourseNumber = Int32.Parse(newValues["CourseNumber"].ToString());
    row.Location = newValues["Location"].ToString();
    row.Name = newValues["Name"].ToString();

    myDatasource.YoDatas.Add(row);
}

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    if ( ! IsPostBack )
    {
        for (int i = 0; i <= 8; i++)
        {
            YoData row = new YoData();
            row.CourseID = i + 1;
            row.CourseNumber = i + 1;
            row.Name = "Name " + (i + 1);
            row.Location = "Country " + (i + 1);

            myDatasource.YoDatas.Add(row);
        }
    } 
    RadGrid1.DataSource = myDatasource.YoDatas;
}

在App_Code中:

  

我使用object而不是DataTable,因为它更容易。

namespace Demo
{
public class ObjectContainer
{
    public ObservableCollection<YoData> YoDatas;


    public ObjectContainer()
    {
        YoDatas = new ObservableCollection<YoData>();
    }

    public ObservableCollection<YoData> get_yoda(int mode = 0)
    {
        return this.YoDatas;
    }
    public void set_yoda(int CourseID, int CourseNumber, string Location, string Name)
    {
        YoData _temp = new YoData(CourseID, CourseNumber, Location, Name);
        this.YoDatas.Add(_temp);
    }

}
public class YoData
{
    public int CourseID { get; set; }
    public int CourseNumber { get; set; }
    public DateTime CourseDate { get; set; }
    public String Location { get; set; }
    public String Name { get; set; }



    public YoData()
    {
        CourseID = 0;
        CourseNumber = 0;
        CourseDate = DateTime.Now;
        Location = "";
        Name = "";
    }

    public YoData(int a, int b, DateTime c, string d, string e)
    {
        CourseID = a;
        CourseNumber = b;
        CourseDate = c;
        Location = d;
        Name = e;
    }
    public YoData(int a, int b, string d, string e)
    {
        CourseID = a;
        CourseNumber = b;
        CourseDate = DateTime.Now;
        Location = d;
        Name = e;
    }
}
}



2 /。在automatics中,telerik为你做了(后来)

[编辑即将推出....]



这不是最好的解决方案,代码中必定存在一些错误,但它有效。仅使用doc。

完成此操作

我希望这有帮助,
问候,
皮埃尔。