drews1f
asked on
Adding an extra column to a datagrid view
Hello,
I am trying to display a register of data in a datagridview. Like so:
Private Sub RetrieveMortgages()
Dim dbSourceNew = ConfigurationManager.AppSe ttings("db Source")
Dim objConnection As New SqlConnection("Persist Security Info=False;Initial Catalog=" & _
"myo;Data Source=" + dbSourceNew + ";User Id=***;Password=***")
Dim mortAdapter As New SqlDataAdapter("(SELECT mort_date_written,mort_typ e,mort_rep ay_method, " & _
"mort_deal,mort_company,mo rt_no,mort _amount,mo rt_comm,mo rt_settled ,mort_actu al_comm," & _
"mort_comm_recd,mort_comm_ paid,mort_ introducer ,mort_comp leted FROM mortgage WHERE " & _
"mort_application_date IS NOT NULL) UNION " & _
"(SELECT mort_date_written,mort_typ e,mort_rep ay_method, mort_deal, mort_compa ny,mort_no ," & _
"mort_amount,mort_comm,mor t_settled, mort_actua l_comm,mor t_comm_rec d,mort_com m_paid," & _
"mort_introducer,mort_comp leted FROM remortgage WHERE mort_application_date IS NOT NULL) " & _
"ORDER BY mort_application_date DESC", objConnection)
Dim mortDT As New DataTable("mort")
mortAdapter.Fill(mortDT)
grdMortgageRegister.DataSo urce = mortDT
End Sub
I want to insert a column between column(0) - Date Written and
column (1) - Mortgage Type
which will be Name.
How can i adjust my code below to get each rows: mort_client_id
and then get client_name and client_surname from the client table?
Cheers
I am trying to display a register of data in a datagridview. Like so:
Private Sub RetrieveMortgages()
Dim dbSourceNew = ConfigurationManager.AppSe
Dim objConnection As New SqlConnection("Persist Security Info=False;Initial Catalog=" & _
"myo;Data Source=" + dbSourceNew + ";User Id=***;Password=***")
Dim mortAdapter As New SqlDataAdapter("(SELECT mort_date_written,mort_typ
"mort_deal,mort_company,mo
"mort_comm_recd,mort_comm_
"mort_application_date IS NOT NULL) UNION " & _
"(SELECT mort_date_written,mort_typ
"mort_amount,mort_comm,mor
"mort_introducer,mort_comp
"ORDER BY mort_application_date DESC", objConnection)
Dim mortDT As New DataTable("mort")
mortAdapter.Fill(mortDT)
grdMortgageRegister.DataSo
End Sub
I want to insert a column between column(0) - Date Written and
column (1) - Mortgage Type
which will be Name.
How can i adjust my code below to get each rows: mort_client_id
and then get client_name and client_surname from the client table?
Cheers
' Initialize the button column.
Dim nameColumn As New DataGridViewTextBoxColumn
With nameColumn
.HeaderText = "Name"
.Name = "Name"
.Text = "View Details"
End With
' Add the button column to the control.
grdMortgageRegister.Columns.Insert(1, nameColumn)
If the client name and client surname was in the same table then i dont think it was a question to be asked here.
Is there a link between the mortgage table and the client table? A primary and foreign key relationship?
Is there a link between the mortgage table and the client table? A primary and foreign key relationship?
ASKER
i created a foreign key between
mortgage client
mort_client_id -----> client_id
And no client_name and client_surname dont exist in the mortgage table!
mortgage client
mort_client_id -----> client_id
And no client_name and client_surname dont exist in the mortgage table!
You can create a query with both tables !
SELECT mort_date_written, client_name & ' ' & client_surname as clientname, mort_type,mort_repay_metho d,mort_dea l,mort_com pany,mort_ no,mort_am ount,mort_ comm,mort_ settled,mo rt_actual_ comm, mort_comm_recd, mort_comm_paid, mort_introducer, mort_completed
FROM mortgage LEFT JOIN Client on mort_clientID = client_ID
WHERE mort_application_date IS NOT NULL
SELECT mort_date_written, client_name & ' ' & client_surname as clientname, mort_type,mort_repay_metho
FROM mortgage LEFT JOIN Client on mort_clientID = client_ID
WHERE mort_application_date IS NOT NULL
ASKER
OK that is exactly what i need!
The only problem is sometimes mortgages are for 2 people in which case there is two clients surnames and forenames i need to get.
mortgage client
mort_client_id1 ------> client_id
mort_client_id2 ------> client_id
is there anyway to do this or is it impossible? :(
The only problem is sometimes mortgages are for 2 people in which case there is two clients surnames and forenames i need to get.
mortgage client
mort_client_id1 ------> client_id
mort_client_id2 ------> client_id
is there anyway to do this or is it impossible? :(
Do you want to get it in one row or in 2 rows (so a row for each person) ?
I think you would be better of using properly normalised tables so that if you have 3 clients in a mortgage then you could deal with that. To do that, you would need to create a third table. For example
Client:
ClientID, Forename, Surname...
Mortgage:
MortgageID, Date, Amount...
ClientMortgage:
ClientID, MortgageID...
Client:
ClientID, Forename, Surname...
Mortgage:
MortgageID, Date, Amount...
ClientMortgage:
ClientID, MortgageID...
ASKER
in one cell which is multiline preferably
Like CodeCruiser said: I would reconsider your table-design !
ASKER
how would i do this using the table design codecruiser suggested?
That would become a parent child relationship so you would have to deal it differently. You would load the details of a mortgage and then list all the clients. Or you could load the details of a client and list all the mortgages he has.
ASKER
im trying to create a list of all the mortgages ever sold though.
Date Written | CLIENT NAME(S) | Type | Amount | Deal | Settled
so each client name that is involved in that mortgages has to appear in the one cell!
Date Written | CLIENT NAME(S) | Type | Amount | Deal | Settled
so each client name that is involved in that mortgages has to appear in the one cell!
The problem is, if you run a query and join these two tables, there will be two rows returned where there are two clients.
ASKER
what about something like this: ie two left joins. can you do that?
i tried this but it returned nothing in the datagridview
i tried this but it returned nothing in the datagridview
Dim mortAdapter As New SqlDataAdapter("(SELECT mort_date_written,mort_type,mort_repay_method," & _
"mort_deal,mort_company,mort_no,mort_amount,mort_comm,mort_settled,mort_actual_comm," & _
"mort_comm_recd,mort_comm_paid,mort_introducer,mort_completed,mort_application_date,mort_client_id1,mort_client_id2 FROM mortgage WHERE " & _
"mort_application_date IS NOT NULL) UNION " & _
"(SELECT mort_date_written,mort_type,mort_repay_method,mort_deal,mort_company,mort_no," & _
"mort_amount,mort_comm,mort_settled,mort_actual_comm,mort_comm_recd,mort_comm_paid," & _
"mort_introducer,mort_completed,mort_application_date,mort_client_id1,mort_client_id2 FROM remortgage WHERE mort_application_date " & _
"IS NOT NULL) LEFT JOIN (SELECT client_name & ' ' & client_surname as clientname" & _
" FROM client WHERE client_id = mort_client_id1)" & _
" LEFT JOIN (SELECT client_name & ' ' & client_surname as partnername" & _
" FROM client WHERE client_id = mort_client_id2)" & _
" ORDER BY mort_application_date DESC", objConnection)
ASKER
OK this is giving the desired result - NEARLY
The code produces datamembers for c1name and c2name and when i set the column datamember to each it shows the correct names.
So how can i make a new datamember which shows:
c1name c1surname
--
or
--
c1name c1surname
c2name c2surname
?
The code produces datamembers for c1name and c2name and when i set the column datamember to each it shows the correct names.
So how can i make a new datamember which shows:
c1name c1surname
--
or
--
c1name c1surname
c2name c2surname
?
Dim mortAdapter As New SqlDataAdapter("(SELECT mort_id,mort_date_written,mort_type,mort_repay_method," & _
"mort_deal,mort_company,mort_no,mort_amount,mort_comm,mort_settled,mort_actual_comm," & _
"mort_comm_recd,mort_comm_paid,mort_introducer,mort_completed,mort_application_date,mort_client_id1," & _
"mort_client_id2, c1.client_name as c1name, c1.client_surname as c1surname, " & _
"c2.client_name as c2name " & _
"FROM remortgage " & _
"LEFT JOIN client c1 ON c1.client_id = mort_client_id1 " & _
"LEFT JOIN client c2 ON c2.client_id = mort_client_id2 " & _
"WHERE mort_application_date IS NOT NULL) UNION " & _
"(SELECT mort_id,mort_date_written,mort_type,mort_repay_method," & _
"mort_deal,mort_company,mort_no,mort_amount,mort_comm,mort_settled,mort_actual_comm," & _
"mort_comm_recd,mort_comm_paid,mort_introducer,mort_completed,mort_application_date,mort_client_id1," & _
"mort_client_id2, c1.client_name as c1name, c1.client_surname as c1surname, " & _
"c2.client_name as c2name " & _
"FROM remortgage " & _
"LEFT JOIN client c1 ON c1.client_id = mort_client_id1 " & _
"LEFT JOIN client c2 ON c2.client_id = mort_client_id2 " & _
"WHERE mort_application_date IS NOT NULL)" & _
" ORDER BY mort_application_date DESC", objConnection)
ASKER
maybe i can change something in the designer to get the desired result?
if i try to add something other than c1name i get no results in the grid :(
if i try to add something other than c1name i get no results in the grid :(
'
'cName
'
Me.cName.DataPropertyName = "c1name"
Me.cName.HeaderText = "Client(s) Name"
Me.cName.Name = "cName"
Me.cName.ReadOnly = True
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If so, you can easily change your query to add the column and display it in your gird
Dim mortAdapter As New SqlDataAdapter("(SELECT mort_date_written, client_name & ' ' & Client_surname as ClientName, mort_type,mort_repay_metho
"mort_deal,mort_company,mo
"mort_comm_recd,mort_comm_
"mort_application_date IS NOT NULL) UNION " & _
"(SELECT mort_date_written, client_name & ' ' & Client_surname as ClientName, mort_type,mort_repay_metho
"mort_amount,mort_comm,mor
"mort_introducer,mort_comp
"ORDER BY mort_application_date DESC", objConnection)