0

我对asp.net 非常陌生,对telerik 尤其陌生。我正在尝试使用以下代码创建一个gridview,只是为了看看它是如何工作的,但是我遇到了一个奇怪的问题,我将commanditemdisplay添加到顶部,并且每次我出于某种原因单击它时它都会添加“添加按钮”它每行多次添加编辑和删除按钮,不确定我做错了什么,但有人可以帮我解决这个问题。

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["GridData"] == null)
        {
            DataTable table = GetTable();
            Session.Add("GridData", table);
        }
        DefineGridStructure();
    }


    private void DefineGridStructure()
    {

        DataTable current = (DataTable)Session["GridData"];

        RadGrid grid = new RadGrid();
        grid.ID = "RadGrid1";
        grid.DataSource = current;
        grid.AutoGenerateEditColumn = true;
        grid.AutoGenerateDeleteColumn = true;
        grid.AllowAutomaticInserts = true;
        grid.Width = Unit.Percentage(100);
        grid.PageSize = 15;
        grid.AllowPaging = true;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;
        grid.MasterTableView.Width = Unit.Percentage(100);
        grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
        grid.MasterTableView.DataKeyNames = new string[] { "RowNumber" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "RowNumber";
        boundColumn.HeaderText = "RowNumber";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Size";
        boundColumn.HeaderText = "Size";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Description";
        boundColumn.HeaderText = "Description";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Quantity";
        boundColumn.HeaderText = "Quantity";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Duration";
        boundColumn.HeaderText = "Duration";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "DurationType";
        boundColumn.HeaderText = "DurationType";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Amount";
        boundColumn.HeaderText = "Amount";
        grid.MasterTableView.Columns.Add(boundColumn);
        grid.DataBind();
        PlaceHolder1.Controls.Add(grid);
    }



    static DataTable GetTable()
    {
        //
        // Here we create a DataTable with a few columns.
        //
        // Create Datatable to store all colums
        DataTable dt = new DataTable();
        DataRow dr = null;

        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Size", typeof(string)));
        dt.Columns.Add(new DataColumn("Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Quantity", typeof(string)));
        dt.Columns.Add(new DataColumn("Unit", typeof(string)));
        dt.Columns.Add(new DataColumn("Duration", typeof(string)));
        dt.Columns.Add(new DataColumn("DurationType", typeof(string)));
        dt.Columns.Add(new DataColumn("Amount", typeof(string)));

        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Size"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Quantity"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Duration"] = string.Empty;
        dr["DurationType"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
        return dt;
    }
} 
4

1 回答 1

0

请尝试使用以下代码片段。

private void DefineGridStructure()
{

    DataTable current = (DataTable)Session["GridData"];

    RadGrid grid = new RadGrid();
    grid.ID = "RadGrid123";
    grid.DataSource = current;
    if (!IsPostBack)
    {
        grid.AutoGenerateEditColumn = true;
        grid.AutoGenerateDeleteColumn = true;
    }
    grid.Width = Unit.Percentage(100);
    grid.PageSize = 15;
    grid.AllowPaging = true;
    grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
    grid.AutoGenerateColumns = false;
    grid.MasterTableView.Width = Unit.Percentage(100);
    grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
    grid.MasterTableView.DataKeyNames = new string[] { "RowNumber" };
    GridBoundColumn boundColumn = new GridBoundColumn();
    boundColumn.DataField = "RowNumber";
    boundColumn.HeaderText = "RowNumber";
    grid.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Size";
    boundColumn.HeaderText = "Size";
    grid.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Description";
    boundColumn.HeaderText = "Description";
    grid.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Quantity";
    boundColumn.HeaderText = "Quantity";
    grid.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Duration";
    boundColumn.HeaderText = "Duration";
    grid.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "DurationType";
    boundColumn.HeaderText = "DurationType";
    grid.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Amount";
    boundColumn.HeaderText = "Amount";
    grid.MasterTableView.Columns.Add(boundColumn);
    grid.DataBind();
    PlaceHolder1.Controls.Add(grid);
}
于 2013-09-13T11:30:23.433 回答