How to retrieve dataitem

19 posts, 0 answers
  1. thdwlgP
    thdwlgP avatar
    34 posts
    Member since:
    Mar 2009

    Posted 30 Apr 2009 Link to this post

    I've databound a Radcombobox to a dataset.Table.

    When I select an item from the combobox, I get the SelectedItem but how do I get the object as a dataRow?
    Is there a way? I know you can do this with RadGrid but if there is a way to do the same with radcombobox let me know.

    Thanks
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 May 2009 Link to this post

    Hi,

    You can access the DataRow for the selected item in a RadComboBox using the following code:
    c#:
    DataRow[] tableRows = dataTable.Select("id=" + RadComboBox1.SelectedValue); 

    Also check out the following link to understand better:
    DataTable.Select Method (String)

    Thanks
    Princy.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Brian Cauley
    Brian Cauley avatar
    10 posts
    Member since:
    May 2006

    Posted 05 May 2009 Link to this post

    That only works if you store the datatable in the session, does the RadComboBox expose the datasource it was bound to?
  5. thdwlgP
    thdwlgP avatar
    34 posts
    Member since:
    Mar 2009

    Posted 05 May 2009 Link to this post

    Sorry if I wasnt clear but that wasnt the answer I was looking for.

    Assume I have the following DataTable. My when I bind the DataTable to the combobox, and if the index is changed, How do I get the DataRow back without using any other global temporary datatable variable to refer the position. When I call
    myCombobox.SelectedItem.DataItem

    the dataitem is always null...  if my selectedItem.Text == "Bob",  I want the DataItem to return the DataRow like dr as below... so I can get the dr["Address"] = "123 Ave"  address.  Any ideas?

    1 //In aspx 
    2 <telerik:RadComboBox ID="myCombobox" runat="server" Filter="Contains"  
    3 DataTextField="Name" DataValueField="ID" Width="220"
    4 </telerik:RadComboBox> 
    5  
    6 //In code 
    7 DataTable dt = new DataTable(); 
    8 dt.Columns.Add(new DataColumn("ID"typeof(string))); 
    9 dt.Columns.Add(new DataColumn("Name"typeof(string))); 
    10 dt.Columns.Add(new DataColumn("Address"typeof(DateTime))); 
    11  
    12 DataRow dr = dt.NewRow(); 
    13 dr["ID"] = "12345"
    14 dr["Name"] = "Bob"
    15 dr["Address"] = "123 Ave"
    16 dt.Rows.Add(dr); 
    17  
    18 DataRow dr2 = dt.NewRow(); 
    19 dr2["ID"] = "6123"
    20 dr2["Name"] = "Mary"
    21 dr2["Address"] = "123 Ave #333"
    22 dt.Rows.Add(dr2); 
    23  
    24 myCombobox.Datasource = dt; 
    25 myCombobox.DataBind(); 
    26  
    27 //on mycombobox index changed 
    28 protected void myCombobox_OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e) 
    29
    30   DataRow dr = myCombobox.SelectedItem.DataItem as DataRow; 
    31




  6. Arnstein
    Arnstein avatar
    12 posts
    Member since:
    Mar 2008

    Posted 12 Feb 2010 Link to this post

    I also have the same problem. I know I can add .Attributes to each item in the ItemDataBound() event and read those .Attributes in the SelectedIndexChanged() event, but I really would like to have access to the complete .DataItem, like I have in RadGrid.

    Regards,
    Arnstein
  7. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 17 Feb 2010 Link to this post

    Hello,

    Please take a look at the following example based on previously posted code:

    <telerik:RadComboBox ID="myCombobox" runat="server" Filter="Contains"  
        AutoPostBack="true" DataTextField="Name" DataValueField="ID" Width="220"
        OnSelectedIndexChanged="myCombobox_OnSelectedIndexChanged">
    </telerik:RadComboBox>

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Address", typeof(string)));
     
            DataRow dr = dt.NewRow();
            dr["ID"] = "12345";
            dr["Name"] = "Bob";
            dr["Address"] = "123 Ave";
            dt.Rows.Add(dr);
     
            DataRow dr2 = dt.NewRow();
            dr2["ID"] = "6123";
            dr2["Name"] = "Mary";
            dr2["Address"] = "123 Ave #333";
            dt.Rows.Add(dr2);
            myCombobox.DataSource = dt;
            myCombobox.DataBind();
        }
    }
    protected void myCombobox_OnSelectedIndexChanged
        (object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RadComboBoxItem item = myCombobox.FindItemByValue(e.Value);
    }

    Note the way RadComboBoxItem is retrieved. More about RadComboBoxItems you can find here.

    Another important issue is that the SelectedIndexChanged event does not fire unless the AutoPostBack property is True - more about this topic find in this article.

    Of course if you prefer to add more properties to an item, you can use Attributes collection to store them.

    Best regards,
    Kalina
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  8. Arnstein
    Arnstein avatar
    12 posts
    Member since:
    Mar 2008

    Posted 01 Mar 2010 Link to this post

    Thank you, Kalina.
    I'm afraid this doesn't completely answer my question. First of all, let's say DataTable dt comes from a database server. When I have found the selected RadComboBoxItem (item), I have direct access to “ID” and “Name”. But how do I find the value of “Address” without going back to the database server again?
    In RadGrid, I could have read it from the .DataItem...

    Best regards,
    Arnstein

     

  9. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 01 Mar 2010 Link to this post

    Hi Arnstein,

    Thank you for the clarification.

    I suggest you use the Attributes collection of RadComboBoxItems. You can use this collection to expand the information stored in them.

    I made a few changes to your code to illustrate this approach:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Address", typeof(string)));
     
            DataRow dr = dt.NewRow();
            dr["ID"] = "12345";
            dr["Name"] = "Bob";
            dr["Address"] = "Address 1";
            dt.Rows.Add(dr);
     
            DataRow dr2 = dt.NewRow();
            dr2["ID"] = "6123";
            dr2["Name"] = "Mary";
            dr2["Address"] = "Address 2";
            dt.Rows.Add(dr2);
     
            foreach (DataRow dataRow in dt.Rows)
            {
                RadComboBoxItem item = new RadComboBoxItem();
     
                item.Text = (string)dataRow["Name"];
                item.Value = dataRow["ID"].ToString();
                item.Attributes.Add("Address", dr["Address"].ToString());
     
                myCombobox.Items.Add(item);
                item.DataBind();
            }
        }
    }

    protected void myCombobox_OnSelectedIndexChanged
    (object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
    {
        RadComboBoxItem item = myCombobox.FindItemByValue(e.Value);
        string address = item.Attributes["Address"];
         
    }

    Best wishes,
    Kalina
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  10. Rory
    Rory avatar
    159 posts
    Member since:
    Jul 2007

    Posted 23 Mar 2010 Link to this post

    For us using Attributes isn't appropriate because it has to download all the attributes client side and if it contains special characters it can throw erros. Therefore we would stiil like to know why the SelectedItem.DataItem is null? We want to be able to access the DataItem. Like this code that errors:

    String strTemplate = DataBinder.Eval(cboTemplates.SelectedItem.DataItem, "Template").ToString();

     

  11. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 26 Mar 2010 Link to this post

    Hello Rory,

    In which event are you trying to access the DataItem?

    You can try retrieving the DataItem in the ItemDataBound server-side event - more details you can find here.

    Kind regards,
    Kalina
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  12. Rory
    Rory avatar
    159 posts
    Member since:
    Jul 2007

    Posted 26 Mar 2010 Link to this post

    Hi Kalina, I was trying to access the original DataSource in the SelectedIndexChanged. So for instance you select an item and when it posts to the server on that event use the SelectedItem to get at the original datasource which has more information than just the Text and Value of the selecteditem but the original datasoure seems to no longer be part of the item. Thanks.
  13. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 01 Apr 2010 Link to this post

    Hi Rory,

    The DataItem and original DataSource are not available in the OnSelectedIndexChanged event.

    You can retrieve them only in data binding events (e.g. ItemDataBound). The alternative is to use custom attributes or save the data in the session or an xml file.

    Kind regards,
    Kalina
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  14. Velmurugan
    Velmurugan avatar
    37 posts
    Member since:
    Aug 2011

    Posted 10 Nov 2011 Link to this post

    hi kalina.....

              i am velmurugan....i am using radlistbox in rad grid.
             how to get multiple selected item from radlistbox and selected should be stored in database with single ID =001
            001= item1
            001= item2
            001= item3
            001= item4

    but when displying the datas in radgrid like  001= item1, item2,item3,item4 using split a string.
    how to use when inserting,updating in database,radgrid.
    plz help me ....urgently need for my projects




  15. Cat Cheshire
    Cat Cheshire avatar
    128 posts
    Member since:
    Jun 2010

    Posted 11 Nov 2011 Link to this post

    What control do you use - combo or listbox?
    In general you cannot use items with same values in both controls.
  16. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 11 Nov 2011 Link to this post

    Hello  Velmurugan ,

    Please let me know by which way you bind your grid and list.

    like: DataTable,DataSet,List,SQldatasource.....etc

    Thanks,
    Jayesh Goyani
  17. Velmurugan
    Velmurugan avatar
    37 posts
    Member since:
    Aug 2011

    Posted 11 Nov 2011 Link to this post

    hi...

         i am using radlistbox with multiple selection
  18. Velmurugan
    Velmurugan avatar
    37 posts
    Member since:
    Aug 2011

    Posted 11 Nov 2011 Link to this post

    hi.....

    using sqldatasource..

      <asp:SqlDataSource ID="SqlDataSource5" runat="server"  ConnectionString="<%$ ConnectionStrings:StudentConnectionString %>"
        SelectCommand="SELECT PERIOD  FROM PERIODMASTER" >   
          </asp:SqlDataSource>

    plz goyani help me........... 
  19. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 11 Nov 2011 Link to this post

    Hello,

    can you please send your data structure also ?

    Thanks,
    Jayesh Goyani
  20. Velmurugan
    Velmurugan avatar
    37 posts
    Member since:
    Aug 2011

    Posted 22 Nov 2011 Link to this post

    hi...

        CREATE TABLE PERIODMASTER( [PERIODCODE] [int] IDENTITY(1,1) NOT NULL, [PERIOD] [nvarchar](50) NULL) ;

    PERIODCODE   PERIOD
    -------------------   -----------
    1                          Period1
    2                           Period2
    3                           Period3
    4                           Period4
    5                           Period15


Back to Top
UI for ASP.NET Ajax is Ready for VS 2017