Add new record to Telerik RadGrid

喜夏-厌秋 提交于 2019-12-11 19:54:10

问题


I have the following Telerik RadGrid, which I'm using in C#:

<MasterTableView Width="100%" EditMode= "InPlace" ClientDataKeyNames="menuID" CommandItemDisplay= "Top">
    <Columns>
        <telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name" UniqueName="Name"></telerik:GridBoundColumn>
        <telerik:GridTemplateColumn UniqueName="Type" HeaderText="Type">
            <ItemTemplate>
                <asp:TextBox ID="Type" Text='<%# DataBinder.Eval(Container.DataItem, "Type") %>' runat="server"></asp:TextBox>
            </ItemTemplate>
            <EditItemTemplate>
            </EditItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridTemplateColumn UniqueName="List" HeaderText="List">
            <ItemTemplate>
                <asp:CheckBox ID="List" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "List") %>' />
            </ItemTemplate>
            <EditItemTemplate>
            </EditItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridTemplateColumn UniqueName="loadAtStart" HeaderText="loadAtStart">
            <ItemTemplate>
                <asp:CheckBox ID="loadAtStart" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "loadAtStart") %>' />
            </ItemTemplate>
            <EditItemTemplate>
            </EditItemTemplate>
        </telerik:GridTemplateColumn>
    </Columns>
</MasterTableView>

The grid is populated with data, and working normally when editing the data.

But when I click on the Add New Record button provided by Telerik, then an empty row is added without any TextBox or CheckBox in the columns to edit in the new Row added. It's just an empty row. I presume that I have to create the controls dynamically in the called ItemDataBound event, but I didn't manage to find the actual code for this.

How do I solve this?


回答1:


Please try with the below code snippet.

ASPX

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
    AllowFilteringByColumn="true" AllowPaging="true" OnItemCommand="RadGrid1_ItemCommand">
    <PagerStyle AlwaysVisible="true" />
    <MasterTableView DataKeyNames="UniqueID" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Save All" OnClick="Button1_Click" />

ASPX.CS

public partial class aaaa : System.Web.UI.Page
{

    public List<Employee> lstEmployee
    {
        get
        {
            if (Session["lstEmployee"] != null)
            {
                return (List<Employee>)Session["lstEmployee"];
            }
            else
            {
                return new List<Employee>();
            }
        }
        set
        {
            Session["lstEmployee"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Employee> list = new List<Employee>();

            Employee obj = new Employee();
            obj.ID = 1;
            obj.Name = "Name1";
            obj.UniqueID = Guid.NewGuid();
            list.Add(obj);

            obj = new Employee();
            obj.ID = 2;
            obj.Name = "Name2";
            obj.UniqueID = Guid.NewGuid();
            list.Add(obj);

            lstEmployee = list;
        }
    }

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = lstEmployee;
    }

    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName)
        {
            saveAllData();
            lstEmployee.Insert(0, new Employee() { UniqueID = Guid.NewGuid() });
            e.Canceled = true;
            RadGrid1.Rebind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        saveAllData();
    }

    protected void saveAllData()
    {
        //Update Session
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            Guid UniqueID = new Guid(item.GetDataKeyValue("UniqueID").ToString());
            Employee emp = lstEmployee.Where(i => i.UniqueID == UniqueID).First();
            emp.Name = (item.FindControl("TextBox1") as TextBox).Text;
        }
    }

}

public class Employee
{
    public Guid UniqueID { get; set; }
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
    public int weeknumber { get; set; }
}


来源:https://stackoverflow.com/questions/22834299/add-new-record-to-telerik-radgrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!