Help me with binding please
I have a database with pers records, another database with finance. Currently, I have a form with pers info on top, and a dataview with the finance below. I have added a relation to the two and when you change the navigate thru the pers records, the finance records in the dataview change accordingly. Now I want to have some textboxes that will show whatever row is selected in the dataview. I thought I could just bind them to the same bindingsource as the dataview but that does not seem to be working. Also, once that is working; Next I will want to figure out how to update the finance records if changes occur. The pers records cannot be changed.
Here is my code so far to load the form as described.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DocCtrl
{
public partial class frmAllClaims : Form
{
DataGridTableStyle t = new DataGridTableStyle();
string strControl;
private BindingSource masterBindingSource = new BindingSource();
private BindingSource detailsBindingSource = new BindingSource();
public frmAllClaims()
{
InitializeComponent();
}
private void frmAllClaims_Load(object sender, EventArgs e)
{
// Connection strings.
String connectionString =
"Data Source=SCE-SCE-DSVR02D;Initial Catalog=TrainingManagementDB;Integrated Security=True";
String connectionString2 =
"Data Source=SCE-SCE-DSVR02D;Initial Catalog=DocCtrl;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
SqlConnection connection2 = new SqlConnection(connectionString2);
// Create a DataSet.
DataSet data = new DataSet();
data.Locale = System.Globalization.CultureInfo.InvariantCulture;
// Add data from the Pers table to the DataSet.
SqlDataAdapter masterDataAdapter = new
SqlDataAdapter("SELECT Last_Name, Service_Number, First_Name, Initials, Rank, Status, COS_In, MOC, Squadron, Gender, Element FROM Persdata ORDER BY Last_Name", connection);
masterDataAdapter.Fill(data, "PersData");
// Add data from the Claims table to the DataSet.
SqlDataAdapter detailsDataAdapter = new
SqlDataAdapter("select * from Claims", connection2);
detailsDataAdapter.Fill(data, "Claims");
// Establish a relationship between the two tables.
DataColumn primarykey = data.Tables["Persdata"].Columns["Service_Number"];
DataColumn foreignkey = data.Tables["Claims"].Columns["SN"];
bool bConstraints = true;
DataRelation relation = new DataRelation("CustomersOrders", primarykey, foreignkey, bConstraints);
// Add the relation to the DataSet.
data.Relations.Add(relation);
// Bind the master data connector to the Persdata table.
masterBindingSource.DataSource = data;
masterBindingSource.DataMember = "PersData";
// Bind the pers textboxs to the masterBindingSource
dgClaims.DataSource = masterBindingSource;
dgClaims.DataMember = "CustomersOrders";
txtSN.DataBindings.Add("text", masterBindingSource, "Service_Number");
txtRank.DataBindings.Add("text", masterBindingSource, "Rank");
txtLast.DataBindings.Add("text", masterBindingSource, "Last_Name");
txtFirst.DataBindings.Add("text", masterBindingSource, "First_Name");
txtInit.DataBindings.Add("text", masterBindingSource, "Initials");
txtCOS.DataBindings.Add("text", masterBindingSource, "COS_In");
txtMOC.DataBindings.Add("text", masterBindingSource, "MOC");
txtGender.DataBindings.Add("text", masterBindingSource, "Gender");
txtElement.DataBindings.Add("text", masterBindingSource, "Element");
txtSqn.DataBindings.Add("text", masterBindingSource, "Squadron");
txtStatus.DataBindings.Add("text", masterBindingSource, "Status");
bindingNavigator1.BindingSource = masterBindingSource;
detailsBindingSource.DataSource = data;
detailsBindingSource.DataMember = "Claims";
//bind the claim textboxs to the row selected
// txtClaimNum.DataBindings.Add("text", detailsBindingSource, "#");
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Re: Help me with binding please
Alright, since I didn't find any help with that, how about if I ask this.
If you had two databases on the same SQL server. One is personnel that has a members number. The second table is finance and it also has the same members number and you wish to display the members name at the top of the form with the members number. Below is a datagrid that shows all the finance records for that member. You wish to be able to navigate through the members with the use of the navigator bar. How would you complete this task so you can update the finance records?