PDA

Click to See Complete Forum and Search --> : dynamic datagrid


Marivic
Nov 15th, 2002, 03:59 AM
How do you create a dynamic datagrid in vb.net?

Edneeis
Nov 15th, 2002, 10:22 AM
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.

Marivic
Nov 15th, 2002, 08:00 PM
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:


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

Marivic
Nov 15th, 2002, 09:03 PM
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

Edneeis
Nov 15th, 2002, 09:30 PM
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.

Marivic
Nov 15th, 2002, 10:49 PM
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

husain
Nov 15th, 2002, 11:02 PM
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.

Marivic
Nov 15th, 2002, 11:49 PM
Hi,

Just check out this link:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=225&lngWId=10