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

How to retrieve dataitem

18 Answers 969 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
thdwlgP
Top achievements
Rank 1
thdwlgP asked on 30 Apr 2009, 10:19 PM
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

18 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 May 2009, 01:33 PM
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.

0
Brian Cauley
Top achievements
Rank 1
answered on 05 May 2009, 05:24 PM
That only works if you store the datatable in the session, does the RadComboBox expose the datasource it was bound to?
0
thdwlgP
Top achievements
Rank 1
answered on 05 May 2009, 05:34 PM
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?

//In aspx 
<telerik:RadComboBox ID="myCombobox" runat="server" Filter="Contains"  
DataTextField="Name" DataValueField="ID" Width="220"
</telerik:RadComboBox> 
 
//In code 
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(DateTime))); 
 
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(); 
 
//on mycombobox index changed 
protected void myCombobox_OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e) 
  DataRow dr = myCombobox.SelectedItem.DataItem as DataRow; 




0
Arnstein
Top achievements
Rank 2
answered on 12 Feb 2010, 10:39 AM
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
0
Kalina
Telerik team
answered on 17 Feb 2010, 12:29 PM
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.
0
Arnstein
Top achievements
Rank 2
answered on 01 Mar 2010, 01:08 PM

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

 

0
Kalina
Telerik team
answered on 01 Mar 2010, 03:25 PM
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.
0
Rory
Top achievements
Rank 1
answered on 23 Mar 2010, 09:41 PM
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();

 

0
Kalina
Telerik team
answered on 26 Mar 2010, 03:02 PM
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.
0
Rory
Top achievements
Rank 1
answered on 26 Mar 2010, 04:53 PM
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.
0
Kalina
Telerik team
answered on 01 Apr 2010, 09:17 AM
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.
0
Velmurugan
Top achievements
Rank 1
answered on 10 Nov 2011, 07:14 AM
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




0
Cat Cheshire
Top achievements
Rank 1
answered on 11 Nov 2011, 10:46 AM
What control do you use - combo or listbox?
In general you cannot use items with same values in both controls.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 11 Nov 2011, 12:49 PM
Hello  Velmurugan ,

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

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

Thanks,
Jayesh Goyani
0
Velmurugan
Top achievements
Rank 1
answered on 11 Nov 2011, 12:53 PM
hi...

     i am using radlistbox with multiple selection
0
Velmurugan
Top achievements
Rank 1
answered on 11 Nov 2011, 12:57 PM
hi.....

using sqldatasource..

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

plz goyani help me........... 
0
Jayesh Goyani
Top achievements
Rank 2
answered on 11 Nov 2011, 02:23 PM
Hello,

can you please send your data structure also ?

Thanks,
Jayesh Goyani
0
Velmurugan
Top achievements
Rank 1
answered on 22 Nov 2011, 12:28 PM
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


Tags
ComboBox
Asked by
thdwlgP
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Brian Cauley
Top achievements
Rank 1
thdwlgP
Top achievements
Rank 1
Arnstein
Top achievements
Rank 2
Kalina
Telerik team
Rory
Top achievements
Rank 1
Velmurugan
Top achievements
Rank 1
Cat Cheshire
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or