|
-
May 27th, 2011, 04:49 PM
#1
Thread Starter
New Member
[RESOLVED] Problem with databinding in c#
Goodevening,
i am trying to implement a 2 table database with a parent child relationship. Then i put in my form two gridviews. One for the parent and on for the child. The parent has 2 columns and an id and the child has 3 columns and the id of the parent. I post the code:
private void Form1_Load(object sender, EventArgs e)
{
// Dataset->BindingSource->Gridview
ownerBindingSource.DataSource = axisDataSet;
ownerBindingSource.DataMember = "owner";
ownerDataGridView.DataSource = ownerBindingSource;
//Child binding source
xyzBindingSource.DataSource = ownerBindingSource;
xyzBindingSource.DataMember = "FK_owner_xyz";
xyzDataGridView.DataSource = xyzBindingSource;
ownerBindingNavigator.BindingSource = ownerBindingSource;
// Bind the textboxcontrols with the table
addtextboxbind_com();
// TODO: This line of code loads data into the 'axisDataSet.owner' table. You can move, or remove it, as needed.
ownerTableAdapter.Fill(axisDataSet.owner);
// TODO: This line of code loads data into the 'axisDataSet.xyz' table. You can move, or remove it, as needed.
xyzTableAdapter.Fill(axisDataSet.xyz);
}
The problem is that even if the filter works ok i only see the id column of the child in the gridview when i should see 4 columns?What am i missing?
Thanks everybody,
ioigoume
-
May 27th, 2011, 06:37 PM
#2
Re: Problem with databinding in c#
Try setting:
C# Code:
xyzBindingSource.DataMember = "FK_owner_xyz";
to the child instead or whatever the target table name is.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
May 28th, 2011, 12:17 AM
#3
Re: Problem with databinding in c#
Follow the CodeBank link in my signature and check out my thread on Parent/Child Data-binding.
-
May 28th, 2011, 01:22 AM
#4
Thread Starter
New Member
Re: Problem with databinding in c#
Thank you both for the answers.
As far as the link of the codebank is proposed i have already checked it before posting and i checked it a works with comboboxes, the problem is that i can not make it work using the gridview.
As far as the suggetion is concerned that's what i use. I link the child bindingsource with the parent bindingsource and then as datamember i set the foreigh key.
The problem is that i see only the foreign key column and not all the columns of the child table. On the other hand i bind the dataset with textbox controls and by moving next i can see all the data. The proble is the grid, why i can not see all the child columns?
ioigoume
-
May 28th, 2011, 02:24 AM
#5
Re: Problem with databinding in c#
First up, it's not a GridView. A GridView is an ASP.NET server control. It's a DataGridView, which is a WinForms control. If you get in the habit of using the correct names for things all the time then you won't get in the habit of causing confusion when it matters.
As for the issue, the fact that you're using grids is pretty much irrelevant. Here is the code from my CodeBank thread converted to work with two DataGridView controls:
Code:
private void Form1_Load(object sender, EventArgs e)
{
// Get the data. The DataSet must contain a Parent table,
// a Child table and a ParentChild relation between them.
DataSet data = this.GetDataSet();
// Bind the parent source to the parent table.
this.bindingSource1.DataSource = data;
this.bindingSource1.DataMember = "Parent";
// Bind the child source to the relationship.
this.bindingSource2.DataSource = this.bindingSource1;
this.bindingSource2.DataMember = "ParentChild";
// Bind the parent control to the parent source.
this.dataGridView1.DataSource = this.bindingSource1;
// Bind the child control to the child source.
this.dataGridView2.DataSource = this.bindingSource2;
}
private DataSet GetDataSet()
{
DataSet data = new DataSet();
DataTable parent = this.GetParentTable();
DataTable child = this.GetChildTable();
data.Tables.Add(parent);
data.Tables.Add(child);
// Add a relationship between the ID of the parent
// table and the ParentID of the child table.
data.Relations.Add("ParentChild",
parent.Columns["ID"],
child.Columns["ParentID"]);
return data;
}
private DataTable GetParentTable()
{
DataTable table = new DataTable("Parent");
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Rows.Add(1, "Parent1");
table.Rows.Add(2, "Parent2");
table.Rows.Add(3, "Parent3");
return table;
}
private DataTable GetChildTable()
{
DataTable table = new DataTable("Child");
table.Columns.Add("ID", typeof(int));
table.Columns.Add("ParentID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Rows.Add(1, 1, "Child1");
table.Rows.Add(2, 1, "Child2");
table.Rows.Add(3, 1, "Child3");
table.Rows.Add(4, 2, "Child4");
table.Rows.Add(5, 2, "Child5");
table.Rows.Add(6, 2, "Child6");
table.Rows.Add(7, 3, "Child7");
table.Rows.Add(8, 3, "Child8");
table.Rows.Add(9, 3, "Child9");
return table;
}
The ONLY difference between that code and what's in the CodeBank is that I changed this part:
Code:
// Bind the parent control to the parent source.
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "ID";
this.comboBox1.DataSource = this.bindingSource1;
// Bind the child control to the child source.
this.comboBox2.DisplayMember = "Name";
this.comboBox2.ValueMember = "ID";
this.comboBox2.DataSource = this.bindingSource2;
to this:
Code:
// Bind the parent control to the parent source.
this.dataGridView1.DataSource = this.bindingSource1;
// Bind the child control to the child source.
this.dataGridView2.DataSource = this.bindingSource2;
Everything else is EXACTLY the same. I tested it to make sure and it worked exactly as it was supposed to.
-
May 28th, 2011, 02:32 AM
#6
Thread Starter
New Member
Re: Problem with databinding in c#
Thank you for the reply,
this is the code i use, see yourself
private void Form1_Load(object sender, EventArgs e)
{
// Parent Binding source
ownerBindingSource.DataSource = axisDataSet;
ownerBindingSource.DataMember = "owner";
//Child binding source
xyzBindingSource.DataSource = ownerBindingSource;
xyzBindingSource.DataMember = "FK_owner_xyz";
// Parent to grid
ownerDataGridView.DataSource = ownerBindingSource;
// Child to grid
xyzDataGridView.DataSource = xyzBindingSource;
// Parent navigator
ownerBindingNavigator.BindingSource = ownerBindingSource;
// Bind the textboxcontrols with the table
addtextboxbind_com();
// TODO: This line of code loads data into the 'axisDataSet.owner' table. You can move, or remove it, as needed.
ownerTableAdapter.Fill(axisDataSet.owner);
// TODO: This line of code loads data into the 'axisDataSet.xyz' table. You can move, or remove it, as needed.
xyzTableAdapter.Fill(axisDataSet.xyz);
}
where axisdataset is the table wrapper generated by the designer.
It seems that i am making the mistake in a different spot of my program.
ioigoume
-
May 28th, 2011, 08:43 AM
#7
Thread Starter
New Member
Re: Problem with databinding in c#
Let me post some new code that makes me wonder. I said that my array has 4 columns and that is what i see in the dataset design. But when i read the code from the form designer only one row is generated.Here is the code:
Code:
// xyzDataGridView
//
this.xyzDataGridView.AutoGenerateColumns = false;
this.xyzDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.xyzDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.iDDataGridViewTextBoxColumn});
this.xyzDataGridView.DataSource = this.xyzBindingSource;
this.xyzDataGridView.Location = new System.Drawing.Point(12, 174);
this.xyzDataGridView.Name = "xyzDataGridView";
this.xyzDataGridView.Size = new System.Drawing.Size(445, 111);
this.xyzDataGridView.TabIndex = 1;
//
// iDDataGridViewTextBoxColumn
//
this.iDDataGridViewTextBoxColumn.DataPropertyName = "ID";
this.iDDataGridViewTextBoxColumn.HeaderText = "ID";
this.iDDataGridViewTextBoxColumn.Name = "iDDataGridViewTextBoxColumn";
this.iDDataGridViewTextBoxColumn.ReadOnly = true;
I tried to put manually the 3 more columns myself taking as an example the the parent grid but it does not seem to work. I believe that i did something small wrong and i can not find it.Any ideas?
The code that makes the paren child filter is
Code:
private void Form1_Load(object sender, EventArgs e)
{
// Parent Binding source
ownerBindingSource.DataSource = axisDataSet;
ownerBindingSource.DataMember = "owner";
//Child binding source
xyzBindingSource.DataSource = ownerBindingSource;
xyzBindingSource.DataMember = "FK_owner_xyz";
// Parent to grid
ownerDataGridView.DataSource = ownerBindingSource;
// Child to grid
xyzDataGridView.DataSource = xyzBindingSource;
// Parent navigator
ownerBindingNavigator.BindingSource = ownerBindingSource;
// Bind the textboxcontrols with the table
addtextboxbind_com();
// TODO: This line of code loads data into the 'axisDataSet.owner' table. You can move, or remove it, as needed.
ownerTableAdapter.Fill(axisDataSet.owner);
// TODO: This line of code loads data into the 'axisDataSet.xyz' table. You can move, or remove it, as needed.
xyzTableAdapter.Fill(axisDataSet.xyz);
}
Hope someone knows better than me!
ioigoume
-
May 28th, 2011, 12:28 PM
#8
Thread Starter
New Member
Re: Problem with databinding in c#
I found the problem.
It seems that the columns of the datagridview were gone. Apparently i did something wrong.
So i deleted the whole datagridviewcontrol and i re-entered it. After that everything ok. So simple and yet it took me half a day
Thanx everybody for your replies!
ioigoume
-
May 29th, 2011, 03:14 AM
#9
Re: Problem with databinding in c#
If your have found the answer to the question please mark the thread "Resolved".
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|