|
-
Nov 15th, 2002, 04:59 AM
#1
Thread Starter
Addicted Member
dynamic datagrid
How do you create a dynamic datagrid in vb.net?
-
Nov 15th, 2002, 11:22 AM
#2
What do you mean by dynamic? You mean add a new datagrid at runtime?
Dim dg as New DataGrid()
Me.Controls.Add(dg)
Or do you mean populate it? You can bind it to any dataset or any classes that implement the IListSource or IList interface or any interfaces that implement them.
-
Nov 15th, 2002, 09:00 PM
#3
Thread Starter
Addicted Member
Hi Edneeis,
Kindly bear with me for a while. I really wanted to understand how to do this:
Here is my scenario:
Initially, I populated my datagrid at design time. Dragging SQLDataAdapter from the toolbar and generating a dataset. Creating it that way made my app not portable. So if I install my app with a different connection string, then I would compile it again with the new connection. I wouldn't want it to happen so I made use of the app.config so I could replace my connection string in one file without recompiling the whole application. Am I correct?
So I revised my form and delete the objects (SQLDataAdapter, SQLConnection, Dataset) from the form so I will just read the connection from the app.config. So far so good.
Now I'm faced with the problem how to populate the datagrid. I understand that since I didn't make use of the SQLDataAdapter, SQLConnection, Dataset at design time, I will have to code this.
Currently what I did is:
I drag a datagrid(Datagrid1) object to the form. And I have this is the page_load event:
VB Code:
Private Sub vendor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnSQL As New SqlConnection()
cnSQL.ConnectionString = ConfigurationSettings.AppSettings("VendorConnect")
cnSQL.Open()
Dim drSQL As New SqlDataAdapter("select userid, userpass from usermaster where usertype='V'", cnSQL)
Dim ds As New DataSet()
'da = New DataSet()
ds.Clear()
drSQL.Fill(ds, "usermaster")
DataGrid1.DataSource = ds
DataGrid1.BackColor = Color.AliceBlue
DataGrid1.RowHeaderWidth = 15
DataGridTextBoxColumn1.Width = 150
cnSQL.Close()
cnSQL.Dispose()
Catch Exp As SqlException
MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
End Try
'SqlDataAdapter1.Fill(Dsusermaster1)
End Sub
When I run this, I got on the grid a plus sign that when clicked displayed the dataset "usermaster". Clicking it again displays the datagrid. What I wanted is to have the records display just like when I populated it at design time.
Could you please help how can I accomplish it? Do I have to define also the TableStyle and GridColumnStyle?
Finally, is this the correct way to accomplish this?
Thanks a lot.
Marivic
-
Nov 15th, 2002, 10:03 PM
#4
Thread Starter
Addicted Member
[resolved]
Hi,
I've got to have this:
DataGrid1.DataMember = "usermaster"
And for those newbies like me, you have to define datagridtablestyle to customize datagrid and datagridtextboxcolumn to customize datagrid column.
HTH,
Marivic
-
Nov 15th, 2002, 10:30 PM
#5
Glad to see you got it. Yes you have to set the datagrid to a specific table to show, setting it to the dataset (which can have multiple tables) isn't enough.
-
Nov 15th, 2002, 11:49 PM
#6
Thread Starter
Addicted Member
[resolved]
Hi,
I've got to have this:
DataGrid1.DataMember = "usermaster"
And for those newbies like me, you have to define datagridtablestyle to customize datagrid and datagridtextboxcolumn to customize datagrid column.
HTH,
Marivic
-
Nov 16th, 2002, 12:02 AM
#7
Junior Member
Re: [resolved]
Originally posted by Marivic
And for those newbies like me, you have to define datagridtablestyle to customize datagrid and datagridtextboxcolumn to customize datagrid column.
I would appreciate it if you posted an example on how to do that. Thanks.
-
Nov 16th, 2002, 12:49 AM
#8
Thread Starter
Addicted Member
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
|