|
I know of two ways to get a DGV automatically display a collection of data:
1.
stuff the data in a DataTable, then simply establish a binding between DT and DGV. Once the DT is fully populated, the DGV columns, their HeaderText, Width, etc are all set automatically.
2.
or have a collection of some type (not: object) with a number of public properties, then give the DGV a number of columns and bind each column to a property of the type in the collection, using DataGridViewColumn.DataPropertyName[^]
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
|
|
|
|
|
Hi Luc,
thank you for your hints . I tried both ways, and after a little bit of fiddling I could make either way work. On the way I found an interesting piece of code (how to convert a generic list into a datatable[^]), which might also be interesting for others.
Have a nice weekend, regards
Mick
|
|
|
|
|
you're welcome.
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
|
|
|
|
|
The class that is in the list, is it using public variables i.e.
public class foobar
public id as integer = 0
public name as string = string.empty
end class
if so I dont know why it does it but that always leaves the gridview empty until your define properties e.g.
public class foobar
private _id as integer = 0
private _name as string = string.empty
public property ID as integer
end property
public property Name as string
end property
end class
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
Data binding uses reflection, it looks for properties with the specified names.
Luc Pattyn [My Articles] Nil Volentibus Arduum
Fed up by FireFox memory leaks I switched to Opera and now CP doesn't perform its paste magic, so links will not be offered. Sorry.
|
|
|
|
|
Thanks Luc
Thats my something learnt for today now I'm off to make snow angels
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
Thank you, Simon - this was exactly one of my basic errors! After defining properties instead of the fields in the class, everything worked out.
Have a nice weekend with a lot of snow angels and , regards
Mick
|
|
|
|
|
Your welcome Michael and have a good weekend yourself
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
hi, i'm trying to do a looping like this,
if i input 5 then,
1
121
12321
1234321
123454321
1234321
12321
121
1
my teacher code structure is like below,
confuse level : 99999
for (loop the row)
select case
case 1
for
if
end if
next
case 2
if
end if
end case
next
what i do,
Private Sub btnView1_Click(sender As System.Object, e As System.EventArgs) Handles btnView1.Click
Dim input As Integer = Nothing
Dim spaces As String = Nothing
Dim numbers As String = Nothing
'##INPUT
input = CInt(InputBox("Insert Max Value", "Value"))
For a As Integer = 1 To (input * 2) - 1
'##SPACES
If a <= input Then
For b As Integer = 1 To (input - a)
spaces += " "
Next
Else
For b As Integer = 1 To (a - input)
spaces += " "
Next
End If
'##NUMBERS
If a <= input Then
For c As Integer = 1 To a
numbers += c.ToString()
Next
Else
For c As Integer = 1 To ???
numbers += c.ToString()
Next
End If
'##OUTPUT
ListBox1.Items.Add(spaces + numbers)
spaces = ""
numbers = ""
Next
End Sub
can someone correct '???' in my code?
thanks before..
modified 7-Feb-12 22:14pm.
|
|
|
|
|
Are you getting an error, or is the output not what you expect ?
My advice is free, and you may get what you paid for.
|
|
|
|
|
??? = a step -1
but that will only give you half of the diamond
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
I try this in vb6 and it really a nice code
Private Sub Command1_Click()
List1.Clear
Dim inp As Integer
Dim spaces As String
Dim numbers As String
inp = CInt(InputBox("Insert Max Value", "Value"))
Dim a As Integer
Dim b As Integer
Dim c As Integer
For a = 1 To (inp * 2) - 1
If a <= inp Then
For b = 1 To (inp - a)
spaces = spaces + " "
Next
Else
For b = 1 To (a - inp)
spaces = spaces + " "
Next
End If
If a <= inp Then
For c = 1 To a
numbers = numbers & c
Next
Else
For c = 1 To (inp * 2) - a
numbers = numbers & c
Next
End If
List1.AddItem spaces + numbers
spaces = ""
numbers = ""
Next
End Sub
|
|
|
|
|
well thankyou very much, but i hope you don't use that anymore..
vb6 is in it's way to hell..
once again, thankyou for the logic..
|
|
|
|
|
Apologize if I post this on the wrong forum.
In ACCESS, I have Form Open event
that will connect to DB1 and datas will show on the detail records.
The form has a Uniquetable = "tableEmp".
Now, I have a option button that will
change database to DB2,
Note: DB1 and DB2 all tables are the same.
In the form details I have checkbox, if I
will checked the checkbox the tables update automatically,
and it will update because of the uniquetable but If
I will change the database the tables will not update.
what's cause of this? thanks in advance,.
C# コードMicrosoft End User
2000-2008
「「「「「「「「「「「「「「「「「「「「「「「「「「「「
The best things in life are free
」」」」」」」」」」」」」」」」」」」」」」」」」」」」
|
|
|
|
|
Check if the database db2 has the same table.
If it does, the primary key that you update the table on may not be present in this table (in the second database).
|
|
|
|
|
thanks. i check the db2 and it has the same table.
is my approach correct?
i mean connecting other database in an access form.
here is my snippet code: ACCESS FORM
Private Sub Form_Open(Cancel As Integer)
Dim ssql As String
ssql = "SELECT [tbl1].id, [tbl1].code,[tbl1].mflag, [tbl2].cdnum, [tbl3].bname"
ssql = ssql & " FROM [tbl1] INNER JOIN"
ssql = ssql & " [tbl2] ON"
ssql = ssql & " [tbl1].id = [tbl2].id INNER JOIN"
ssql = ssql & " [tbl3] ON [tbl1].code = [tbl3].code"
ssql = ssql & " WHERE [tbl1].mdate ='2011/10/12'"
Me.RecordSource = ssql
Me.UniqueTable = "tbl2"
End Sub
Private Sub optSel_AfterUpdate()
Dim ssql As String
If Me.optSel = 1 Then
ssql = "SELECT [tbl1].id, [tbl1].code,[tbl1].mflag, [tbl2].cdnum, [tbl3].bname"
ssql = ssql & " FROM [tbl1] INNER JOIN"
ssql = ssql & " [tbl2] ON"
ssql = ssql & " [tbl1].id = [tbl2].id INNER JOIN"
ssql = ssql & " [tbl3] ON [tbl1].code = [tbl3].code"
ssql = ssql & " WHERE [tbl1].mdate ='2011/10/12'"
Else
ADODisconn
ADOConDB2
ssql = "SELECT [tbl2].id,[tbl2].code,[tbl2].mflag,[tbl2].cdnum,[tbl3].bname"
ssql = ssql & " FROM [tbl2] INNER JOIN [tbl3] ON "
ssql = ssql & " [tbl2].code = [tbl3].code"
ssql = ssql & " WHERE [tbl2].id > 160"
End If
Me.RecordSource = ssql
Me.UniqueTable = "tbl2"
End Sub
C# コードMicrosoft End User
2000-2008
「「「「「「「「「「「「「「「「「「「「「「「「「「「「
The best things in life are free
」」」」」」」」」」」」」」」」」」」」」」」」」」」」
|
|
|
|
|
There is no UPDATE query in this code so its really difficult to figure out.
|
|
|
|
|
no. the update is on the form.
Using the Continuous Forms
The same as GridiView in visual basic.
In detail of continuous form, I have textboxes and checkboxes, if I check the checkbox,
the table is automatically updated. Database update is not on the code, its on the form.
C# コードMicrosoft End User
2000-2008
「「「「「「「「「「「「「「「「「「「「「「「「「「「「
The best things in life are free
」」」」」」」」」」」」」」」」」」」」」」」」」」」」
|
|
|
|
|
C#Coudou wrote: is my approach correct?
i mean connecting other database in an access form.
here is my snippet code: ACCESS FORM
It looks like VB.NET, but I don't see any OleDbCommand's. Are you using the "old" recordsets?
FWIW; I don't see any code where you bind the data to the UI, nor where you re-open the new connection. The best suggestion I can give is to use the OleDb provider to access your database.
Bastard Programmer from Hell
|
|
|
|
|
nope, this is not vb.net, this is MS Access, that is why you cannot see an oledbcommand.yes, I'm using old recordsets.
You can't see open connection because in the Form1 properties, it has recordsource, also in form open.
If i will use oledb connection, how can i add the data's in my continuous form?
This project is an .adp(microsoft access project),which means if you open the myproject.adp, it is already connected to database.
C# コードMicrosoft End User
2000-2008
「「「「「「「「「「「「「「「「「「「「「「「「「「「「
The best things in life are free
」」」」」」」」」」」」」」」」」」」」」」」」」」」」
|
|
|
|
|
C#Coudou wrote: nope, this is not vb.net, this is MS Access, that is why you cannot see an oledbcommand.yes, I'm using old recordsets.
My apologies, I assumed VB.NET - that's the name of the forum.
C#Coudou wrote: If i will use oledb connection, how can i add the data's in my continuous form?
I don't think that will work; those reports are built for use within Access itself.
C#Coudou wrote: This project is an .adp(microsoft access project),which means if you open the myproject.adp, it is already connected to database.
Not even sure whether Access allows switching the current connection.
It's been some time ago that I worked in Access, and we didn't "switch" the connection to another database - we decided it was easier to import the data from other databases, and manipulate it in the current connection.
Bastard Programmer from Hell
|
|
|
|
|
hi,
i and updating my data gridview cell pragmatically.data is showing in the cell. but not updating but if i typed same value in that cell it is updating.
my code is
Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=SQG-133;User Id=" & My.Forms.LoginForm.UsernameTextBox.Text & ";Password=" & My.Forms.LoginForm.PasswordTextBox.Text & ";Initial Catalog=" & My.Forms.LoginForm.tb_database.Text & "")
'update statement----
conn = da_inv.SelectCommand.Connection
da_inv.UpdateCommand = New SqlCommand()
da_inv.UpdateCommand.Connection = conn
da_inv.UpdateCommand.CommandText = "UPDATE dbo.dt_finnance_value_adition SET bank_doc_ref_no=@bank_doc_ref_no WHERE fund_id = @fund_id"
With da_inv.UpdateCommand.Parameters
.Add("@fund_id", SqlDbType.Int, 0, "fund_id")
.Add("@bank_doc_ref_no", SqlDbType.NChar, 10, "bank_doc_ref_no")
End With
'update statement end
da_inv.Update(ds_inv, "inv_table")
|
|
|
|
|
The parameters you are adding contain string literals, you should be using the variables, trying to stuff "fund_id" into an .Int parameter is going to cause problems!
.Add("@fund_id", SqlDbType.Int, 0, "fund_id")
.Add("@bank_doc_ref_no", SqlDbType.NChar, 10, "bank_doc_ref_no")
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I tried this code to display the data in dGV on the form, but it display the datat as a inclined,not as it show in the DGV , row after row.
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
For j = 0 To DataGridView1.ColumnCount - 1
Dim g As Graphics
g = Me.CreateGraphics()
Dim str As String = Me.DataGridView1.Rows(i).Cells(j).Value
Dim y, X As Integer
y += 20
X += 30
g.DrawString(str, New Font("Times New Roman", 12, FontStyle.Regular), Brushes.DarkGreen, X, y)
Next
Next
|
|
|
|
|
Dim y, X As Integer
y += 20
X += 30
What are these values supposed to represent? I think you need to initialise them with something meaningful.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|