【问题标题】:Telerik Radgrid row delete and restore the data on adding new row dynamicallyTelerik Radgrid 行删除和恢复动态添加新行的数据
【发布时间】:2016-09-12 14:05:40
【问题描述】:

我下面的代码只是为了显示带有“添加新课程”按钮的 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();
        }
    }

【问题讨论】:

    标签: asp.net telerik telerik-grid


    【解决方案1】:



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


    正如 Telerik 管理员在官方论坛上告诉您的那样,请按照我在 8 天前告诉您的方式阅读文档。

    在演示中查看魔术,在文档中了解更多信息:


    让我为你做!

    下面的例子是在一个新的/干净的telerik项目中开发的,你可以复制过去试试。

    1/。使用高级绑定又名 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>
    

    在代码隐藏中:

        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 中:

    我使用对象而不是 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/.在自动装置中,telerik 为你做(稍后)

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



    这不是最好的解决方案,代码中肯定有一些错误,但它确实有效。仅使用文档即可完成此操作。

    希望对你有帮助,
    问候,
    皮埃尔。

    【讨论】:

    • 是否真的可以在按钮单击时添加新行,当用户在新行中输入新数据(尚未保存)时,保持用户尝试添加另一个新行时输入的数据(回发发生在这里)到 radgrid。最后,在用户添加了所需的行之后,点击保存,所有行数据都保存到数据库中。友善的建议。我已经达到了可以在按钮单击时添加行但不维护回发数据的地步。
    • 尝试查看状态/会话以存储数据..然后在需要时保存到数据库
    • @BVidhya,我在官方论坛上看到你的帖子还需要帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多