Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Databind Dataset to Label Control
 

Databind Dataset to Label Control

Is it possible to databind the results from a dataset query to a label control. The query I have written against my dataset will only ever return a single 1 field record and I want to bind this to a label control. I have got to the stage of running my dataset query using a tableadapter, but I am struggling what to do next....help please!!

01.Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load   
02.    Dim PublicationsAdapter As New PublicationsDSTableAdapters.t_PublicationsTableAdapter   
03.  
04.    If Not (Request.QueryString("pubid") Is Nothing) Then  
05.        Panel_1.Visible = False  
06.        Panel_2.Visible = True  
07.        pubid.Text = Request.QueryString("pubid")   
08.        pubid.Visible = True  
09.  
10.        pubname.Text = PublicationsAdapter.GetDataByPubName(Request.QueryString("pubid")).PublicationNameColumn   
11.  
12.    End If  
13.End Sub  
suggy1982  Wednesday, September 30, 2009 8:07 PM

Hi,

 

You can use simple binding to bind dataset to the label.

            DataSet dataset = new DataSet();

            dataset.Tables.Add("datatable01");

 

            dataset.Tables["datatable01"].Columns.Add("col01");

            dataset.Tables["datatable01"].Columns.Add("col02");

            dataset.Tables["datatable01"].Columns.Add("col03");

 

            dataset.Tables["datatable01"].Rows.Add("22", "bb", "2b");

            dataset.Tables["datatable01"].Rows.Add("11", "aa", "1a");

            dataset.Tables["datatable01"].Rows.Add("33", "cc", "3c");

 

            label1.DataBindings.Add("Text", dataset.Tables["datatable01"], "col01", true, DataSourceUpdateMode.OnPropertyChanged);

 

Control.DataBindings Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.databindings.aspx

 

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Sunday, October 04, 2009 1:13 PM

Hi,

 

You can use simple binding to bind dataset to the label.

            DataSet dataset = new DataSet();

            dataset.Tables.Add("datatable01");

 

            dataset.Tables["datatable01"].Columns.Add("col01");

            dataset.Tables["datatable01"].Columns.Add("col02");

            dataset.Tables["datatable01"].Columns.Add("col03");

 

            dataset.Tables["datatable01"].Rows.Add("22", "bb", "2b");

            dataset.Tables["datatable01"].Rows.Add("11", "aa", "1a");

            dataset.Tables["datatable01"].Rows.Add("33", "cc", "3c");

 

            label1.DataBindings.Add("Text", dataset.Tables["datatable01"], "col01", true, DataSourceUpdateMode.OnPropertyChanged);

 

Control.DataBindings Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.databindings.aspx

 

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Sunday, October 04, 2009 1:13 PM

You can use google to search for other answers

Custom Search

More Threads

• Arraylist is populated using a custom class. Listbox Datasource set to the ArrayList. Error: Object must implement IConvertible
• Hooking into the dataset auto generated code
• wierd 'insert into' behavior
• Support for paging in datagridview
• NullReferenceException on DataSet.Clear used involving databinding
• Problem Capturing Tab Key Pressed
• Row addition in DATA GRID: Where to update to Data Source?
• Binding the "value type" data to DataGridView
• How to get updated rows when grid binded to a binding list collection?
• cell formatting