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

Pass parameter to NeedDataSource

3 Answers 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brad
Top achievements
Rank 1
Brad asked on 24 Sep 2013, 07:09 PM
Is it possible to pass a parameter to

RadGrid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource(Parameter i want to pass));

  void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e, DataTable dt)
    {
        DataTable current = dt;
        RadGrid grid = (RadGrid)sender;
        grid.DataSource = current;
    }

I want to be able to pass it a DataTable as a parameter. How would i be able to do that?

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 25 Sep 2013, 05:26 AM
Hello,

AS my experience with the .Net framework and Telerik controls, this thing is not possible.
 
Can you please elaborate scenario so we can try to achieve this with another way?

Thanks,
Jayesh Goyani
0
Brad
Top achievements
Rank 1
answered on 25 Sep 2013, 02:38 PM
Thanks for your response. Here's the situation, i have a class that creates a radgrid dynamically, I am trying to accomplish the following:
first load the page, and for it to check if session["Tables"] exist, if not create it and then load the inv.DefineGridStructure method to create the grid. That works fine.

Now where the issue comes in, is when i click a button to add another grid, i want it to display the current grid on top, lets say in this case Grid number 2, and then display the previous grids on the bottom, in this case just 1. When i click it and it creates the grid and displays it fine, but the issue is, it will always load the same datasource for all grids and not the right one, for each grid, it seems that in the needDatasource event, its only grabbing the current session and loading that grid, which is the issue, that is why i wanted to pass the actual datatable it should load to that.

Below is the following code:

invoicer.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
/// <summary>
/// Summary description for invoicer
/// </summary>
public class invoicer
{
    public invoicer()
    {
        //
        // TODO: Add constructor logic here
        //
  
    }
 
 
    public class MyTemplate : ITemplate
    {
        private string colname;
        protected Label lControl;
        public MyTemplate(string cName)
        {
            colname = cName;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
            lControl = new Label();
            lControl.ID = "Label-DurationType";
            lControl.DataBinding += new EventHandler(lControl_DataBinding);
            container.Controls.Add(lControl);
        }
 
        public void lControl_DataBinding(object sender, EventArgs e)
        {
            Label l = (Label)sender;
            GridDataItem container = (GridDataItem)l.NamingContainer;
            l.Text = ((DataRowView)container.DataItem)[colname].ToString() + "<br />";
        }
    }
 
    public class MyEditTemplate : IBindableTemplate
    {
        public void InstantiateIn(Control container)
        {
            GridDataItem item = ((GridDataItem)(container.NamingContainer));
            DropDownList drop = new DropDownList();
            drop.ID = "DurationType-DDL";
            drop.DataSource = (DataTable)GetTableForDropDown();
            drop.DataTextField = "DurationType";
            drop.DataValueField = "DurationType";
            container.Controls.Add(drop);
        }
 
        public System.Collections.Specialized.IOrderedDictionary ExtractValues(System.Web.UI.Control container)
        {
            OrderedDictionary od = new OrderedDictionary();
            od.Add("DurationType", ((DropDownList)(((GridDataItem)(container)).FindControl("DurationType-DDL"))).DataValueField);
            return od;
        }
    }
 
    void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable current = (DataTable)HttpContext.Current.Session[GetSession()];
        RadGrid grid = (RadGrid)sender;
        grid.DataSource = current;
    }
 
 
 
    protected void grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem editItem = (GridEditableItem)e.Item;
            DropDownList ddl = (DropDownList)editItem.FindControl("DurationType-DDL");
            ddl.DataSource = (DataTable)GetTableForDropDown();
            ddl.DataTextField = "DurationType";
            ddl.DataValueField = "DurationType";
            ddl.SelectedIndex = editItem.ItemIndex;
            ddl.SelectedValue = DataBinder.Eval(editItem.DataItem, "DurationType").ToString(); // To get the selected value        
  
        }
    }
 
 
    public void DefineGridStructure(int i, PlaceHolder ph, DataTable dt)
    {
        RadGrid grid = new RadGrid();
        grid.ID = "RadGridPH" + i.ToString();
        grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
        grid.DataSource = dt;
        grid.AutoGenerateEditColumn = true;
        grid.AutoGenerateDeleteColumn = true;
        grid.AllowAutomaticInserts = false;
        grid.Width = Unit.Percentage(100);
        grid.PageSize = 15;
        grid.AllowPaging = true;
        grid.AllowFilteringByColumn = true;
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        grid.AutoGenerateColumns = false;
        grid.MasterTableView.Width = Unit.Percentage(100);
        grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
        grid.AllowAutomaticDeletes = false;
        grid.AllowAutomaticUpdates = false;
        grid.ItemDataBound += new GridItemEventHandler(grid_ItemDataBound);
        grid.InsertCommand += grid_InsertCommand;
        grid.DeleteCommand += grid_DeleteCommand;
        grid.UpdateCommand += grid_UpdateCommand;
        grid.MasterTableView.DataKeyNames = new string[] { "RowNumber" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "RowNumber";
        boundColumn.HeaderText = "RowNumber";
        boundColumn.ReadOnly = true;
        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 = "Unit";
        boundColumn.HeaderText = "Unit";
        grid.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Duration";
        boundColumn.HeaderText = "Duration";
        grid.MasterTableView.Columns.Add(boundColumn);
 
        GridTemplateColumn objGridTemplateColumn = new GridTemplateColumn();
        objGridTemplateColumn.HeaderText = "DurationType";
        objGridTemplateColumn.DataField = "DurationType";
        objGridTemplateColumn.ItemTemplate = new MyTemplate("DurationType");
        objGridTemplateColumn.EditItemTemplate = new MyEditTemplate();
        grid.MasterTableView.Columns.Add(objGridTemplateColumn);
 
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "Amount";
        boundColumn.HeaderText = "Amount";
        grid.MasterTableView.Columns.Add(boundColumn);
        grid.MasterTableView.EditMode = GridEditMode.InPlace;
        ph.Controls.Add(grid);
    }
 
    public void grid_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editItem = e.Item as GridEditableItem;
        Hashtable newValues = new Hashtable();
        newValues["RowNumber"] = (editItem["RowNumber"].Controls[0] as TextBox).Text;
        newValues["Size"] = (editItem["Size"].Controls[0] as TextBox).Text;
        newValues["Description"] = (editItem["Description"].Controls[0] as TextBox).Text;
        newValues["Quantity"] = (editItem["Quantity"].Controls[0] as TextBox).Text;
        newValues["Unit"] = (editItem["Unit"].Controls[0] as TextBox).Text;
        newValues["Duration"] = (editItem["Duration"].Controls[0] as TextBox).Text;
        newValues["DurationType"] = (editItem.FindControl("DurationType-DDL") as DropDownList).SelectedValue;
        newValues["Amount"] = (editItem["Amount"].Controls[0] as TextBox).Text;
        DataTable dtCurrentTable = (DataTable)HttpContext.Current.Session[GetSession()];
 
        foreach (DictionaryEntry entry in newValues)
        {
            dtCurrentTable.Rows[e.Item.ItemIndex][entry.Key.ToString()] = entry.Value;
        }
         
 
    }
 
    public void grid_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem item = e.Item as GridDataItem;
        DataTable dt = (DataTable)HttpContext.Current.Session[GetSession()];
        dt.Rows.Remove(dt.Rows[item.ItemIndex]);
        ResetRowID(dt);
         
    }
 
    public void grid_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        GridEditManager editMan = editedItem.EditManager;
        //Set new values
        Hashtable newValues = new Hashtable();
        //The GridTableView will fill the values from all editable columns in the hash
 
        DataTable dtCurrentTable = (DataTable)HttpContext.Current.Session[GetSession()];
        DataRow dr = null;
        int count = dtCurrentTable.Rows.Count;
        count++;
        dr = dtCurrentTable.NewRow();
        dr["RowNumber"] = count;
        newValues["RowNumber"] = count;
        newValues["Size"] = (editedItem["Size"].Controls[0] as TextBox).Text;
        newValues["Description"] = (editedItem["Description"].Controls[0] as TextBox).Text;
        newValues["Quantity"] = (editedItem["Quantity"].Controls[0] as TextBox).Text;
        newValues["Unit"] = (editedItem["Unit"].Controls[0] as TextBox).Text;
        newValues["Duration"] = (editedItem["Duration"].Controls[0] as TextBox).Text;
        newValues["DurationType"] = (editedItem.FindControl("DurationType-DDL") as DropDownList).SelectedValue;
        newValues["Amount"] = (editedItem["Amount"].Controls[0] as TextBox).Text;
        foreach (DictionaryEntry entry in newValues)
        {
            dr[entry.Key.ToString()] = entry.Value;
        }
        dtCurrentTable.Rows.Add(dr);
         
    }
 
 
 
    public string GetSession()
    {
        string SID = HttpContext.Current.Session["Tables"].ToString();
        return SID;
    }
 
 
   public 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);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 2;
        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);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 3;
        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);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 4;
        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);
 
        dr = dt.NewRow();
        dr["RowNumber"] = 5;
        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;
    }
 
 
    static DataTable GetTableForDropDown()
    {
        //
        // 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("DurationType", typeof(string)));
 
        dr = dt.NewRow();
        dr["DurationType"] = "Hours";
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["DurationType"] = "Days";
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["DurationType"] = "Weeks";
        dt.Rows.Add(dr);
 
        dr = dt.NewRow();
        dr["DurationType"] = "Months";
        dt.Rows.Add(dr);
        return dt;
    }
 
    private void ResetRowID(DataTable dt)
    {
        int rowNumber = 1;
 
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                row[0] = rowNumber;
                rowNumber++;
            }
        }
    }
 
 
}


Default.aspx.cs

public partial class Default8 : System.Web.UI.Page
{
 
    invoicer inv = new invoicer();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Tables"] == null)
        {
            Session.Add("Tables", 1);
             
            DataTable dt = inv.GetTable();
             
            Session.Add(Session["Tables"].ToString(), dt);      
        }
 
        Label lbl = new Label();
        lbl.ID = "LABELS-" + Session["Tables"].ToString();
        lbl.Text = "This is Table in PH1 " + Session["Tables"].ToString();
        PlaceHolder1.Controls.Add(lbl);
        inv.DefineGridStructure((int)Session["Tables"], PlaceHolder1);
    }
 
 
    protected void Button1_Click(object sender, EventArgs e)
    {
     
        
        int next = (int)Session["Tables"];
        next = next + 1;
        Session.Add("Tables", next);
        DataTable dt = inv.GetTable();      
        Session.Add(Session["Tables"].ToString(), dt);
 
        Label lbl = new Label();
        lbl.ID = "LABELS-" + Session["Tables"].ToString();
        lbl.Text = "This is Table 1 in PH2 after button click" + Session["Tables"].ToString();
 
        PlaceHolder2.Controls.Add(lbl);
        inv.DefineGridStructure(1, PlaceHolder2,);
 
        /*
        for (int i = 1; i <= (int)Session["Tables"]; i++)
        {
            Label lbl = new Label();
            lbl.ID = "LABELS-" + i.ToString();
            lbl.Text = "This is Table in PH2 " + i.ToString();
            Button btn = new Button();
            btn.ID = "Button-" + i.ToString();
            btn.Text = "Button-" + i.ToString();
            PlaceHolder2.Controls.Add(lbl);
            PlaceHolder2.Controls.Add(btn);
            //inv.DefineGridStructure(i, PlaceHolder2);
        }
         */
          
    }




0
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Sep 2013, 10:57 AM
Hello,

Please try with the below code snippet.

void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid grid = (RadGrid)sender;
 
        string strID = grid.ID.Replace("RadGridPH", "");
                      // Access ID Here
        DataTable current = (DataTable)HttpContext.Current.Session[GetSession()];
      
        grid.DataSource = current;
    }


Thanks.
Jayesh Goyani
Tags
Grid
Asked by
Brad
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Brad
Top achievements
Rank 1
Share this question
or