Results 1 to 8 of 8

Thread: dynamic datagrid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    dynamic datagrid

    How do you create a dynamic datagrid in vb.net?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145
    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:
    1. Private Sub vendor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.                 Dim cnSQL As New SqlConnection()
    3.         cnSQL.ConnectionString = ConfigurationSettings.AppSettings("VendorConnect")
    4.         cnSQL.Open()
    5.         Dim drSQL As New SqlDataAdapter("select userid, userpass from usermaster where usertype='V'", cnSQL)
    6.         Dim ds As New DataSet()
    7.         'da = New DataSet()
    8.         ds.Clear()
    9.         drSQL.Fill(ds, "usermaster")
    10.         DataGrid1.DataSource = ds
    11.         DataGrid1.BackColor = Color.AliceBlue
    12.         DataGrid1.RowHeaderWidth = 15
    13.         DataGridTextBoxColumn1.Width = 150
    14.  
    15.  
    16.         cnSQL.Close()
    17.         cnSQL.Dispose()
    18.  
    19.  
    20.  
    21.         Catch Exp As SqlException
    22.            MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")
    23.  
    24.           Catch Exp As Exception
    25.              MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
    26.          End Try
    27.  
    28.         'SqlDataAdapter1.Fill(Dsusermaster1)
    29.     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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    [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

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

    [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

  7. #7
    Junior Member husain's Avatar
    Join Date
    Jul 2002
    Location
    United Arab Emirates
    Posts
    31

    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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    145

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width