Use DataTable As Source for BindingSource

Jo15765

Member
Joined
May 1, 2018
Messages
6
Programming Experience
Beginner
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.
 
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.
 
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:
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.
 
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?
 
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");
 
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.

C#:
    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:
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.
 
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.
 
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.
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.

C#:
    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");
        }
 
Back
Top Bottom