Results 1 to 11 of 11

Thread: Use DataTable As Source for BindingSource

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    May 2018
    Posts
    5
    Rep Power
    0

    Use DataTable As Source for BindingSource

    I am new to c# and have drug from the toolbox a bindingnavigator and set the source to the bindingsource. Now the binding source I need to set to my DataTable titled dtNew, but in the properties for the binding source, I do not see the option to set the source to a DataTable.

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,881
    Rep Power
    145
    Quote Originally Posted by Jo15765 View Post
    I [...] have drug from the toolbox a bindingnavigator
    "Dragged", not "drug".

    You can only set the DataSource of the BindingNavigator in the designer if the data source is also added in the designer. That will be the case if you are using a typed DataSet but if you're just creating a DataTable in code then you have to set the DataSource of the BindingSource in code too. Normally, you'd do that right after calling Fill on a data adapter. If you have a DataGridView, you can then bind the BindingSource to it and it will generate columns automatically.

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    May 2018
    Posts
    5
    Rep Power
    0
    Sorry for my bad English friend. I try to use syntax below to add the data source from the code behind but I get compile error

    bindingSource1.DataSource = dtTest;

    And the error is:
    An object reference is required for the non static field,method, or property ‘Form1.bindingSource1’


    Amendment so it seems I can resolve this error if I dynamically create the Binding source like so
    Private static BindingSource bs = new BindingSource();


    But that leads me to - how do I bind this to the BindingNavigator since it was created through code?
    Last edited by Jo15765; 05-02-2018 at 9:45 AM.

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,881
    Rep Power
    145
    If you have added a BindingSource named 'bindingSource1' to your form in the designer then that first line of code should work as is, unless you've done something else bizarre. You definitely don't need to declare a static field. There's not enough information to determine what you've done wrong though.

    You can prove to yourself that it works by creating a new project, adding just a BindingNavigator and BindingSource, then create a DataTable in code and bind it. If you do just that it will work. Something you have or haven't done that you're not explaining is the cause of it not working now.

  5. #5
    .NET Framework
    .NET 4.5
    Join Date
    May 2018
    Posts
    5
    Rep Power
    0
    If I add
    Public static BindingSource bs = new BindingSource();
    Public static BindingNavigator bn = new BindingNavigator();

    Then in the code I set it like this:
    bs.DataSource = dtTest;
    bn.BindingSource = bs;

    But that leads me to an unknown of how do I assign the values in the BindingSource/Navigator to textboxes on my Windows form?

  6. #6
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,881
    Rep Power
    145
    DO NOT DECLARE ANY STATIC VARIABLES! Just add the BindingSource and BindingNavigator to the form in the designer. You can set the BindingSource property of the BindingNavigator in the designer too. You can then set up all the binding in code, e.g.

    myDataAdapter.Fill(myDataTable);

    'Bind the DataTable to the BindingSource.
    bindingSource1.DataSource = myDataTable;

    'Bind the BindingSource to the individual controls.
    givenNameTextBox.DataBindings.Add("Text", bindingSource1, "GivenName");
    familyNameTextBox.DataBindings.Add("Text", bindingSource1, "FamilyName");

  7. #7
    .NET Framework
    .NET 4.5
    Join Date
    May 2018
    Posts
    5
    Rep Power
    0
    Why do you not declare variables static?

    This is the code I am working with (it is assigning variables from SharePoint as I expect...to do list is to verify DataTable is being populated and the text boxes are being populated). I am very new to C# so please be kind if this is the wrong way to do things.

    Code:
        public partial class Form1 : System.Windows.Forms.Form
        {
            public static DataTable dtData = new DataTable();
            public static DataRow newRow = dtData.NewRow();
    		
    		private void Form1_Load(object sender, EventArgs e)
            {
                Query();
                BindData();
            }
    		
    		public static void Query()
            {		
    			dtData.Columns.Add("ID", typeof(string));
    			dtData.Columns.Add("at", typeof(string));
    			dtData.Columns.Add("cm", typeof(string));
    			dtData.Columns.Add("cn", typeof(string));
    			dtData.Columns.Add("an", typeof(string));
    			dtData.Columns.Add("sn", typeof(string));
    			dtData.Columns.Add("ce", typeof(string));
    			
    			foreach (ListItem listItem in items)
    			{
    				newRow = dtData.NewRow();
    				newRow["ID"] = listItem["ID"];
    
    
    				FieldUserValue username = (FieldUserValue)listItem["at"];
    				string un = username.LookupValue;
    				newRow["at"] = un;
    
    
    				var cntnum = listItem["cn"] as FieldLookupValue[];
    				if (cntnum != null)
    				{
    					foreach (var contracts in cntnum)
    					{
    						cns = contracts.LookupValue;
    					}
    					newRow["cn"] = cns;
    				}
    
    
    				FieldLookupValue an = listItem["an"] as FieldLookupValue;
    				an = an.LookupValue;
    				newRow["an"] = an;
    
    
    				var sysname = listItem["sn"] as FieldLookupValue[];
    				if (sysname != null)
    				{
    					foreach (var system in sysname)
    					{
    						sn = system.LookupValue;
    					}
    					newRow["sn"] = sn;
    				}
    
    
    				//Capturing contact email(s)
    				var email = listItem["ce"];
    				newRow["ce"] = email;
    			}
    
    
    			//trying to bind ->
    			bs.DataSource = dtData;
    		}
    		
    		
            public void BindData()
            {
                txtcn.DataBindings.Add("Text", bs, "cn");
                txtan.DataBindings.Add("Text", bs, "an");
                txtsn.DataBindings.Add("Text", bs, "sn");
                txtce.DataBindings.Add("Text", bs, "ce");
            }
    Last edited by Jo15765; 05-03-2018 at 10:40 AM.

  8. #8
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,881
    Rep Power
    145
    Do you know what 'static' actually means? You should only declare something 'static' if you specifically need it to be 'static'.

  9. #9
    .NET Framework
    .NET 4.5
    Join Date
    May 2018
    Posts
    5
    Rep Power
    0
    Quote Originally Posted by jmcilhinney View Post
    Do you know what 'static' actually means? You should only declare something 'static' if you specifically need it to be 'static'.
    Please correct my noobness if I am incorrect here, but I believe a static variable is a variable that can be accessed between classes.

  10. #10
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,881
    Rep Power
    145
    Quote Originally Posted by Jo15765 View Post
    Please correct my noobness if I am incorrect here, but I believe a static variable is a variable that can be accessed between classes.
    It is not. I suggest that you read the relevant documentation. That's what the Help menu on VS is for.

  11. #11
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,881
    Rep Power
    145
    Quote Originally Posted by Jo15765 View Post
    Please correct my noobness if I am incorrect here, but I believe a static variable is a variable that can be accessed between classes.
    In your code below, it's the 'public' keyword that means that field is accessible outside the class. The 'static' keyword does something else. I would suggest that declaring those fields public is a mistake too. There's seems to be no good reason to be accessing then from outside that form.
    Quote Originally Posted by Jo15765 View Post
    Why do you not declare variables static?

    This is the code I am working with (it is assigning variables from SharePoint as I expect...to do list is to verify DataTable is being populated and the text boxes are being populated). I am very new to C# so please be kind if this is the wrong way to do things. The same probably goes for the methods too. Scope should be kept as limited as possible. Don't make something accessible outside a type unless it needs to be accessible. Fields should pretty much never be public.

    Code:
        public partial class Form1 : System.Windows.Forms.Form
        {
            public static DataTable dtData = new DataTable();
            public static DataRow newRow = dtData.NewRow();
    		
    		private void Form1_Load(object sender, EventArgs e)
            {
                Query();
                BindData();
            }
    		
    		public static void Query()
            {		
    			dtData.Columns.Add("ID", typeof(string));
    			dtData.Columns.Add("at", typeof(string));
    			dtData.Columns.Add("cm", typeof(string));
    			dtData.Columns.Add("cn", typeof(string));
    			dtData.Columns.Add("an", typeof(string));
    			dtData.Columns.Add("sn", typeof(string));
    			dtData.Columns.Add("ce", typeof(string));
    			
    			foreach (ListItem listItem in items)
    			{
    				newRow = dtData.NewRow();
    				newRow["ID"] = listItem["ID"];
    
    
    				FieldUserValue username = (FieldUserValue)listItem["at"];
    				string un = username.LookupValue;
    				newRow["at"] = un;
    
    
    				var cntnum = listItem["cn"] as FieldLookupValue[];
    				if (cntnum != null)
    				{
    					foreach (var contracts in cntnum)
    					{
    						cns = contracts.LookupValue;
    					}
    					newRow["cn"] = cns;
    				}
    
    
    				FieldLookupValue an = listItem["an"] as FieldLookupValue;
    				an = an.LookupValue;
    				newRow["an"] = an;
    
    
    				var sysname = listItem["sn"] as FieldLookupValue[];
    				if (sysname != null)
    				{
    					foreach (var system in sysname)
    					{
    						sn = system.LookupValue;
    					}
    					newRow["sn"] = sn;
    				}
    
    
    				//Capturing contact email(s)
    				var email = listItem["ce"];
    				newRow["ce"] = email;
    			}
    
    
    			//trying to bind ->
    			bs.DataSource = dtData;
    		}
    		
    		
            public void BindData()
            {
                txtcn.DataBindings.Add("Text", bs, "cn");
                txtan.DataBindings.Add("Text", bs, "an");
                txtsn.DataBindings.Add("Text", bs, "sn");
                txtce.DataBindings.Add("Text", bs, "ce");
            }

Similar Threads

  1. Question expression to filter bindingsource
    By muro in forum C# General Discussion
    Replies: 5
    Last Post: 05-01-2018, 1:13 PM
  2. Question Deleted row bindingsource error ?
    By ebrahim in forum C# General Discussion
    Replies: 0
    Last Post: 05-30-2015, 12:58 PM
  3. Replies: 3
    Last Post: 04-04-2014, 8:30 PM
  4. How to update the DataSource of a BindingSource?
    By DoJa in forum Windows Forms
    Replies: 1
    Last Post: 04-03-2014, 9:21 PM
  5. Question Copy from datatable to new datatable
    By charlie20 in forum Windows Forms
    Replies: 2
    Last Post: 09-06-2012, 3:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •