This is a migrated thread and some comments may be shown as answers.

Grid command items

2 Answers 21 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 12 Sep 2013, 04:48 PM
Hello,
I am extremely new to asp.net and especially new to telerik. I am trying to create a gridview with the following code, just to see how it works, but i ran into a wierd problem, i added the commanditemdisplay to top, and it adds the "add button" every time i click it for some reason it adds the edit and delete buttons multipe times per row, not sure what i am doing wrong in order to cause that, but can someone please help me with this.

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;
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 13 Sep 2013, 04:00 AM
Hi,

Please try the following code snippet, I have removed the 'grid.DataBind()' from your code and bound the RadGrid in NeedDataSource event.

C#:
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["GridData"] == null)
    {
        DataTable table = GetTable();
        Session.Add("GridData", table);
    }
    DefineGridStructure();
}
private void DefineGridStructure()
{
    RadGrid grid = new RadGrid();
    grid.ID = "RadGrid1";
    grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
    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);
    PlaceHolder1.Controls.Add(grid);
}
 
void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable current = (DataTable)Session["GridData"];
    RadGrid grid = (RadGrid)sender;
    grid.DataSource = current;
}
 
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;
}

Thanks,
Princy.
0
Brad
Top achievements
Rank 1
answered on 16 Sep 2013, 07:42 PM
Awesome, that was perfect, exactly what i needed.

Thank you very much!!!
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Brad
Top achievements
Rank 1
Share this question
or